kcode-kweb-context 0.2.8

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

use std::collections::{BTreeMap, HashMap, HashSet};

pub use kcode_kweb_context_node::{
    Connection, Error, Node, NodeDraft, Result, StagedCreate, format_node,
};
use kcode_kweb_db::NodeId;
use kcode_session_history::{
    Session as HistorySession,
    chatend::{BoxContent, BoxId, BoxState, EventId, PendingId, ToolSlotInput},
};
use serde::Serialize;
use serde_json::{Value, json};
use sha2::{Digest, Sha256};

const KWEB_TOOL_INSTANCE: &str = "kweb";
const CONNECTION_SUMMARIES_PER_BOX: usize = 8;
const CONNECTION_SUMMARIES_LOGICAL_SLOT: &str = "connection-summaries";
const CONNECTION_SUMMARY_IDS_METADATA: &str = "kwebConnectionSummaryIds";

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum BoxKind {
    Loaded,
    Fixed,
    Staged,
    Connections,
}

impl BoxKind {
    pub const fn name(self) -> &'static str {
        match self {
            Self::Loaded => "Kweb loaded node",
            Self::Fixed => "Kweb fixed connection",
            Self::Staged => "Kweb staged node",
            Self::Connections => "Kweb connection map markers",
        }
    }

    pub const fn metadata_name(self) -> &'static str {
        match self {
            Self::Loaded => "loaded",
            Self::Fixed => "fixed",
            Self::Staged => "staged",
            Self::Connections => "connection-summary",
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct BoxSpec {
    logical_slot: String,
    kind: BoxKind,
    text: String,
    stored_node: Option<Node>,
    staged_node: Option<NodeDraft>,
}

#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LoadReport {
    pub requested_id: String,
    pub newly_loaded: bool,
    pub promoted_from_fixed: bool,
    pub new_fixed_ids: Vec<String>,
}

/// One current, effect-free provider-facing Kweb context section.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ProjectionItem {
    /// Stable logical identity used to replace a prior rendering.
    pub key: String,
    /// Human-readable section name.
    pub name: String,
    /// Complete current section text.
    pub text: String,
}

#[derive(Clone, Debug)]
pub struct Context {
    root_node_ids: Vec<String>,
    load_fixed_connections: bool,
    loaded_node_ids: Vec<String>,
    fixed_node_ids: Vec<String>,
    nodes_by_id: BTreeMap<String, Node>,
}

impl Context {
    pub fn new(root_node_ids: Vec<String>) -> Result<Self> {
        Self::with_fixed_connections(root_node_ids, false)
    }

    /// Creates Kweb context with optional legacy full fixed-node loading.
    pub fn with_fixed_connections(
        root_node_ids: Vec<String>,
        load_fixed_connections: bool,
    ) -> Result<Self> {
        if root_node_ids.is_empty() {
            return Err(Error::new("Kweb context requires at least one root node"));
        }
        let mut seen = HashSet::new();
        for id in &root_node_ids {
            canonical_node_id(id)?;
            if !seen.insert(id.clone()) {
                return Err(Error::new("Kweb root node IDs must be distinct"));
            }
        }
        Ok(Self {
            root_node_ids,
            load_fixed_connections,
            loaded_node_ids: Vec::new(),
            fixed_node_ids: Vec::new(),
            nodes_by_id: BTreeMap::new(),
        })
    }

    pub fn root_node_ids(&self) -> &[String] {
        &self.root_node_ids
    }

    pub fn loaded_node_ids(&self) -> &[String] {
        &self.loaded_node_ids
    }

    pub fn fixed_node_ids(&self) -> &[String] {
        &self.fixed_node_ids
    }

    pub const fn loads_fixed_connections(&self) -> bool {
        self.load_fixed_connections
    }

    pub fn full_node_ids(&self) -> Vec<&str> {
        self.loaded_node_ids
            .iter()
            .chain(&self.fixed_node_ids)
            .map(String::as_str)
            .collect()
    }

    pub fn contains_full_node(&self, id: &str) -> bool {
        self.loaded_node_ids.iter().any(|candidate| candidate == id)
            || self.fixed_node_ids.iter().any(|candidate| candidate == id)
    }

    pub fn node(&self, id: &str) -> Option<&Node> {
        self.nodes_by_id.get(id)
    }

    pub fn apply_load(&mut self, requested: Node, fixed: Vec<Node>) -> Result<LoadReport> {
        let requested_id = requested.id.clone();
        let was_loaded = self
            .loaded_node_ids
            .iter()
            .any(|candidate| candidate == &requested_id);
        let was_fixed = self
            .fixed_node_ids
            .iter()
            .any(|candidate| candidate == &requested_id);
        let previous_full = self
            .full_node_ids()
            .into_iter()
            .map(str::to_owned)
            .collect::<HashSet<_>>();
        let expected_fixed = requested
            .fixed_connections
            .iter()
            .map(|connection| connection.id.as_str())
            .filter(|id| *id != requested_id)
            .collect::<HashSet<_>>();
        let provided_fixed = fixed
            .iter()
            .map(|node| node.id.as_str())
            .collect::<HashSet<_>>();
        if self.load_fixed_connections && expected_fixed != provided_fixed {
            return Err(Error::new(format!(
                "load for {requested_id} did not provide exactly its fixed connections"
            )));
        }
        if !self.load_fixed_connections && !fixed.is_empty() {
            return Err(Error::new(format!(
                "load for {requested_id} provided full fixed connections while compatibility mode is disabled"
            )));
        }
        self.nodes_by_id.insert(requested_id.clone(), requested);
        if self.load_fixed_connections {
            for node in fixed {
                self.nodes_by_id.insert(node.id.clone(), node);
            }
        }
        if !was_loaded {
            self.loaded_node_ids.push(requested_id.clone());
        }
        self.rebuild_fixed();
        let new_fixed_ids = self
            .fixed_node_ids
            .iter()
            .filter(|id| !previous_full.contains(*id))
            .cloned()
            .collect();
        Ok(LoadReport {
            requested_id,
            newly_loaded: !was_loaded && !was_fixed,
            promoted_from_fixed: !was_loaded && was_fixed,
            new_fixed_ids,
        })
    }

    pub fn refresh(&mut self, nodes: impl IntoIterator<Item = Node>) -> Result<()> {
        for node in nodes {
            if !self.contains_full_node(&node.id) {
                return Err(Error::new(format!(
                    "cannot refresh unloaded Kweb node {}",
                    node.id
                )));
            }
            self.nodes_by_id.insert(node.id.clone(), node);
        }
        self.rebuild_fixed();
        Ok(())
    }

    pub fn restore(
        &mut self,
        nodes: impl IntoIterator<Item = Node>,
        directly_loaded: Vec<String>,
    ) -> Result<()> {
        self.nodes_by_id.clear();
        for node in nodes {
            self.nodes_by_id.insert(node.id.clone(), node);
        }
        let mut seen = HashSet::new();
        self.loaded_node_ids = directly_loaded
            .into_iter()
            .filter(|id| self.nodes_by_id.contains_key(id) && seen.insert(id.clone()))
            .collect();
        if !self.nodes_by_id.is_empty() && self.loaded_node_ids.is_empty() {
            return Err(Error::new(
                "restored Kweb context contains nodes but no loaded node",
            ));
        }
        self.rebuild_fixed();
        Ok(())
    }

    fn box_specs(
        &self,
        updates: &BTreeMap<String, NodeDraft>,
        creates: &[StagedCreate],
    ) -> Result<Vec<BoxSpec>> {
        let mut specs = self.node_box_specs(updates, creates)?;
        let connections = self.projected_connection_summaries(updates, creates)?;
        specs.push(BoxSpec {
            logical_slot: CONNECTION_SUMMARIES_LOGICAL_SLOT.into(),
            kind: BoxKind::Connections,
            text: format_connection_summary_entries(&connections),
            stored_node: None,
            staged_node: None,
        });
        Ok(specs)
    }

    fn node_box_specs(
        &self,
        updates: &BTreeMap<String, NodeDraft>,
        creates: &[StagedCreate],
    ) -> Result<Vec<BoxSpec>> {
        for id in updates.keys() {
            if !self.contains_full_node(id) {
                return Err(Error::new(format!(
                    "staged update targets unloaded Kweb node {id}"
                )));
            }
        }
        let mut specs = Vec::new();
        for id in &self.loaded_node_ids {
            specs.push(self.full_box(id, BoxKind::Loaded, updates.get(id))?);
        }
        for id in &self.fixed_node_ids {
            specs.push(self.full_box(id, BoxKind::Fixed, updates.get(id))?);
        }
        for create in creates {
            specs.push(BoxSpec {
                logical_slot: create.pending_id.clone(),
                kind: BoxKind::Staged,
                text: format_node(&create.pending_id, &create.data),
                stored_node: None,
                staged_node: Some(create.data.clone()),
            });
        }
        Ok(specs)
    }

    /// Renders the complete current Kweb projection without touching Chatend.
    ///
    /// Full nodes and individual connection summaries have independent stable
    /// keys so ordinary replaceable-state consumers can update only values
    /// whose rendered content changed.
    pub fn projection(
        &self,
        updates: &BTreeMap<String, NodeDraft>,
        creates: &[StagedCreate],
    ) -> Result<Vec<ProjectionItem>> {
        let mut projected = self
            .node_box_specs(updates, creates)?
            .into_iter()
            .map(|spec| {
                Ok(ProjectionItem {
                    key: spec.logical_slot,
                    name: spec.kind.name().to_owned(),
                    text: spec.text,
                })
            })
            .collect::<Result<Vec<_>>>()?;
        projected.extend(
            self.projected_connection_summaries(updates, creates)?
                .into_iter()
                .map(|entry| ProjectionItem {
                    key: format!("{CONNECTION_SUMMARIES_LOGICAL_SLOT}:{}", entry.id),
                    name: "Kweb connection map marker".into(),
                    text: entry.text,
                }),
        );
        Ok(projected)
    }

    /// Reconcile this context's complete provider-facing projection through an
    /// already-open durable Session History handle.
    ///
    /// The returned IDs are the active Kweb boxes whose name or canonical
    /// revision changed, in current projection order.
    pub fn sync_chatend(
        &self,
        journal: &mut HistorySession,
        recorded_at: impl Into<String>,
        updates: &BTreeMap<String, NodeDraft>,
        creates: &[StagedCreate],
    ) -> Result<Vec<BoxId>> {
        let recorded_at = recorded_at.into();
        let previous = kweb_box_versions(journal);
        let (mut desired, connections) = desired_kweb_boxes(self.box_specs(updates, creates)?)?;
        desired.extend(desired_connection_summary_boxes(journal, connections)?);
        reconcile_kweb_slots(journal, &recorded_at, desired)?;
        Ok(changed_kweb_box_ids(journal, &previous))
    }

    /// Cache-preserving LoadNodes reconciliation through general Session
    /// History slot application.
    ///
    /// Existing slots are fed back in exact order without changing layout,
    /// visible representation, occurrence history, name, or retirement. The
    /// returned IDs are only pre-existing active boxes whose canonical content
    /// advanced, in existing-slot order.
    pub fn sync_load_chatend(
        &self,
        journal: &mut HistorySession,
        recorded_at: impl Into<String>,
        updates: &BTreeMap<String, NodeDraft>,
        creates: &[StagedCreate],
    ) -> Result<Vec<BoxId>> {
        let recorded_at = recorded_at.into();
        let (desired_nodes, connections) = desired_kweb_boxes(self.box_specs(updates, creates)?)?;
        let plan = plan_cache_safe_kweb_slots(journal, desired_nodes, connections)?;
        journal
            .apply_tool_slots(&recorded_at, KWEB_TOOL_INSTANCE, plan.slots)
            .map_err(|error| Error::new(format!("applying cache-safe Kweb projection: {error}")))?;
        Ok(plan.advanced_existing)
    }

    fn full_box(&self, id: &str, kind: BoxKind, update: Option<&NodeDraft>) -> Result<BoxSpec> {
        let node = self
            .nodes_by_id
            .get(id)
            .ok_or_else(|| Error::new(format!("missing full Kweb node {id}")))?;
        let data = update.cloned().unwrap_or_else(|| node.draft());
        Ok(BoxSpec {
            logical_slot: id.to_owned(),
            kind,
            text: format_node(id, &data),
            stored_node: Some(node.clone()),
            staged_node: update.cloned(),
        })
    }

    fn projected_connection_summaries(
        &self,
        updates: &BTreeMap<String, NodeDraft>,
        creates: &[StagedCreate],
    ) -> Result<Vec<ConnectionSummaryEntry>> {
        let creates_by_id = creates
            .iter()
            .map(|create| (create.pending_id.as_str(), &create.data))
            .collect::<HashMap<_, _>>();
        let mut summaries = HashMap::new();
        for node in self.nodes_by_id.values() {
            summaries.insert(
                node.id.as_str(),
                (node.short_name.as_str(), node.short_description.as_str()),
            );
            for connection in node
                .fixed_connections
                .iter()
                .chain(&node.recent_connections)
            {
                summaries.entry(connection.id.as_str()).or_insert((
                    connection.short_name.as_str(),
                    connection.short_description.as_str(),
                ));
            }
        }
        let mut connection_ids = Vec::new();
        let mut seen = HashSet::new();
        for id in self.full_node_ids() {
            let node = self
                .nodes_by_id
                .get(id)
                .ok_or_else(|| Error::new(format!("missing full Kweb node {id}")))?;
            if let Some(draft) = updates.get(id) {
                for connection_id in draft
                    .fixed_connections
                    .iter()
                    .chain(&draft.recent_connections)
                {
                    if seen.insert(connection_id.clone()) {
                        connection_ids.push(connection_id.clone());
                    }
                }
            } else {
                for connection in node
                    .fixed_connections
                    .iter()
                    .chain(&node.recent_connections)
                {
                    if seen.insert(connection.id.clone()) {
                        connection_ids.push(connection.id.clone());
                    }
                }
            }
        }
        for create in creates {
            for connection_id in create
                .data
                .fixed_connections
                .iter()
                .chain(&create.data.recent_connections)
            {
                if seen.insert(connection_id.clone()) {
                    connection_ids.push(connection_id.clone());
                }
            }
        }
        let mut entries = Vec::with_capacity(connection_ids.len());
        for id in connection_ids {
            let staged_summary = updates
                .get(&id)
                .or_else(|| creates_by_id.get(id.as_str()).copied())
                .map(|node| (node.short_name.as_str(), node.short_description.as_str()));
            let (name, description) = staged_summary
                .or_else(|| summaries.get(id.as_str()).copied())
                .ok_or_else(|| {
                    Error::new(format!(
                        "connection map marker {id} must resolve to a nonempty node name and map marker"
                    ))
                })?;
            if name.trim().is_empty() || description.trim().is_empty() {
                return Err(Error::new(format!(
                    "connection map marker {id} must resolve to a nonempty node name and map marker"
                )));
            }
            let text = format!("{id} · {name}: {description}");
            entries.push(ConnectionSummaryEntry { id, text });
        }
        Ok(entries)
    }

    fn rebuild_fixed(&mut self) {
        let loaded = self.loaded_node_ids.iter().cloned().collect::<HashSet<_>>();
        if !self.load_fixed_connections {
            self.fixed_node_ids.clear();
            self.nodes_by_id.retain(|id, _| loaded.contains(id));
            return;
        }
        let mut seen = loaded.clone();
        let mut fixed = Vec::new();
        for id in &self.loaded_node_ids {
            let Some(node) = self.nodes_by_id.get(id) else {
                continue;
            };
            for connection in &node.fixed_connections {
                if self.nodes_by_id.contains_key(&connection.id)
                    && seen.insert(connection.id.clone())
                {
                    fixed.push(connection.id.clone());
                }
            }
        }
        self.fixed_node_ids = fixed;
        self.nodes_by_id
            .retain(|id, _| loaded.contains(id) || seen.contains(id));
    }
}

struct DesiredKwebBox {
    logical_slot: String,
    name: String,
    content: BoxContent,
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct ConnectionSummaryEntry {
    id: String,
    text: String,
}

fn desired_kweb_boxes(specs: Vec<BoxSpec>) -> Result<(Vec<DesiredKwebBox>, DesiredKwebBox)> {
    let mut desired = Vec::with_capacity(specs.len());
    let mut connections = None;
    for spec in specs {
        let kind = spec.kind;
        let entry = desired_kweb_box(spec)?;
        if kind == BoxKind::Connections {
            if connections.replace(entry).is_some() {
                return Err(Error::new(
                    "Kweb context produced more than one connection-summary candidate",
                ));
            }
        } else {
            desired.push(entry);
        }
    }
    let connections = connections
        .ok_or_else(|| Error::new("Kweb context produced no connection-summary candidate"))?;
    Ok((desired, connections))
}

fn desired_kweb_box(spec: BoxSpec) -> Result<DesiredKwebBox> {
    let mut metadata = json!({
        "revisionHash": revision_hash(&spec.text),
    });
    if let Some(node) = spec.stored_node {
        metadata["canonicalNodeId"] = json!(node.id);
        metadata["storedNode"] = serde_json::to_value(node)
            .map_err(|error| Error::new(format!("serializing stored Kweb node: {error}")))?;
    }
    if let Some(node) = spec.staged_node {
        metadata["staged"] = json!(true);
        metadata["nodeData"] = serde_json::to_value(node)
            .map_err(|error| Error::new(format!("serializing staged Kweb node: {error}")))?;
    }
    let mut content = BoxContent {
        text: spec.text,
        objects: Vec::new(),
        metadata,
    };
    content.use_concise_header();
    mark_kweb_content(&mut content, &spec.logical_slot, spec.kind.metadata_name());
    Ok(DesiredKwebBox {
        logical_slot: spec.logical_slot,
        name: spec.kind.name().into(),
        content,
    })
}

fn mark_kweb_content(content: &mut BoxContent, logical_slot: &str, role: &str) {
    if !content.metadata.is_object() {
        content.metadata = json!({});
    }
    content.metadata["kwebLogicalSlot"] = json!(logical_slot);
    content.metadata["kwebRole"] = json!(role);
}

fn kweb_logical_slot(state: &BoxState, actual_slot: &str) -> String {
    state
        .canonical
        .content
        .metadata
        .get("kwebLogicalSlot")
        .and_then(Value::as_str)
        .unwrap_or(actual_slot)
        .to_owned()
}

fn kweb_role(content: &BoxContent) -> Option<&str> {
    content.metadata.get("kwebRole").and_then(Value::as_str)
}

fn is_full_node_role(role: Option<&str>) -> bool {
    matches!(role, Some("loaded" | "fixed" | "staged"))
}

fn connection_summary_heading(content: &BoxContent) -> Result<&'static str> {
    ["Connection map markers", "Connection summaries"]
        .into_iter()
        .find(|heading| {
            content
                .text
                .strip_prefix(heading)
                .is_some_and(|body| body.is_empty() || body.starts_with('\n'))
        })
        .ok_or_else(|| Error::new("Kweb connection-summary box has an invalid heading"))
}

fn connection_summary_entries(content: &BoxContent) -> Result<Vec<ConnectionSummaryEntry>> {
    let heading = connection_summary_heading(content)?;
    let body = content
        .text
        .strip_prefix(heading)
        .expect("validated connection-summary heading");
    let body = body
        .strip_prefix('\n')
        .ok_or_else(|| Error::new("Kweb connection-summary box has no body"))?;
    if body == "None." {
        return Ok(Vec::new());
    }

    let mut entries: Vec<ConnectionSummaryEntry> = Vec::new();
    for line in body.split('\n') {
        let identifier = line.split_once(" · ").and_then(|(identifier, _)| {
            let canonical = identifier.parse::<NodeId>().is_ok();
            let pending = PendingId::parse(identifier.to_owned()).is_ok();
            (canonical || pending).then_some(identifier)
        });
        if let Some(identifier) = identifier {
            entries.push(ConnectionSummaryEntry {
                id: identifier.to_owned(),
                text: line.to_owned(),
            });
        } else {
            let entry = entries.last_mut().ok_or_else(|| {
                Error::new("Kweb connection-summary box starts with invalid entry text")
            })?;
            entry.text.push('\n');
            entry.text.push_str(line);
        }
    }

    if let Some(expected) = content
        .metadata
        .get(CONNECTION_SUMMARY_IDS_METADATA)
        .and_then(Value::as_array)
    {
        let expected = expected
            .iter()
            .map(|value| {
                value.as_str().ok_or_else(|| {
                    Error::new("Kweb connection-summary IDs metadata contains a non-string value")
                })
            })
            .collect::<Result<Vec<_>>>()?;
        if expected
            != entries
                .iter()
                .map(|entry| entry.id.as_str())
                .collect::<Vec<_>>()
        {
            return Err(Error::new(
                "Kweb connection-summary IDs metadata does not match its canonical text",
            ));
        }
    }
    Ok(entries)
}

fn format_connection_summary_entries(entries: &[ConnectionSummaryEntry]) -> String {
    format_connection_summary_entries_with_heading("Connection map markers", entries)
}

fn format_connection_summary_entries_with_heading(
    heading: &str,
    entries: &[ConnectionSummaryEntry],
) -> String {
    if entries.is_empty() {
        return format!("{heading}\nNone.");
    }
    format!(
        "{heading}\n{}",
        entries
            .iter()
            .map(|entry| entry.text.as_str())
            .collect::<Vec<_>>()
            .join("\n")
    )
}

fn revision_hash(text: &str) -> String {
    hex::encode(Sha256::digest(text.as_bytes()))
}

fn update_connection_summary_content(
    content: &mut BoxContent,
    logical_slot: &str,
    entries: &[ConnectionSummaryEntry],
) {
    content.text = format_connection_summary_entries(entries);
    mark_kweb_content(content, logical_slot, "connection-summary");
    content.metadata["revisionHash"] = json!(revision_hash(&content.text));
    content.metadata[CONNECTION_SUMMARY_IDS_METADATA] = json!(
        entries
            .iter()
            .map(|entry| entry.id.as_str())
            .collect::<Vec<_>>()
    );
}

fn desired_connection_summary_boxes(
    journal: &HistorySession,
    fresh: DesiredKwebBox,
) -> Result<Vec<DesiredKwebBox>> {
    let mut boxes = Vec::new();
    let mut seen = HashSet::new();
    let mut used_logical_slots = HashSet::new();
    let fresh_entries = connection_summary_entries(&fresh.content)?;
    let fresh_by_id = fresh_entries
        .iter()
        .map(|entry| (entry.id.as_str(), entry.text.as_str()))
        .collect::<HashMap<_, _>>();

    if let Some(tool) = journal.state().tools.get(KWEB_TOOL_INSTANCE) {
        for slot in &tool.slots {
            let state = journal
                .state()
                .box_state(slot.box_id)
                .ok_or_else(|| Error::new("Kweb tool slot box is missing"))?;
            let logical_slot = kweb_logical_slot(state, &slot.slot);
            used_logical_slots.insert(logical_slot.clone());
            if slot.retired
                || state
                    .canonical
                    .content
                    .metadata
                    .get("kwebRole")
                    .and_then(Value::as_str)
                    != Some("connection-summary")
            {
                continue;
            }
            let mut entries = connection_summary_entries(&state.canonical.content)?;
            for entry in &mut entries {
                if let Some(text) = fresh_by_id.get(entry.id.as_str()) {
                    entry.text = (*text).to_owned();
                }
            }
            for entry in &entries {
                seen.insert(entry.id.clone());
            }
            let mut content = state.canonical.content.clone();
            update_connection_summary_content(&mut content, &logical_slot, &entries);
            boxes.push(DesiredKwebBox {
                logical_slot,
                name: fresh.name.clone(),
                content,
            });
        }
    }

    let additions = fresh_entries
        .iter()
        .filter(|entry| seen.insert(entry.id.clone()))
        .cloned()
        .collect::<Vec<_>>();
    let mut next_addition = 0;

    while next_addition < additions.len() || boxes.is_empty() {
        let end = (next_addition + CONNECTION_SUMMARIES_PER_BOX).min(additions.len());
        let entries = additions[next_addition..end].to_vec();
        let mut sequence = boxes.len() + 1;
        let logical_slot = loop {
            let candidate = if sequence == 1 {
                CONNECTION_SUMMARIES_LOGICAL_SLOT.to_owned()
            } else {
                format!("{CONNECTION_SUMMARIES_LOGICAL_SLOT}:{sequence}")
            };
            if used_logical_slots.insert(candidate.clone()) {
                break candidate;
            }
            sequence += 1;
        };
        let mut content = fresh.content.clone();
        update_connection_summary_content(&mut content, &logical_slot, &entries);
        boxes.push(DesiredKwebBox {
            logical_slot,
            name: fresh.name.clone(),
            content,
        });
        next_addition = end;
    }

    Ok(boxes)
}

struct ExistingKwebSlot {
    box_id: BoxId,
    actual_slot: String,
    logical_slot: String,
    retired: bool,
    active: bool,
    name: String,
    content: BoxContent,
}

struct ExistingConnectionBox {
    heading: &'static str,
    entries: Vec<ConnectionSummaryEntry>,
}

struct CacheSafeLoadPlan {
    slots: Vec<ToolSlotInput>,
    advanced_existing: Vec<BoxId>,
}

fn plan_cache_safe_kweb_slots(
    journal: &HistorySession,
    desired_nodes: Vec<DesiredKwebBox>,
    fresh_connections: DesiredKwebBox,
) -> Result<CacheSafeLoadPlan> {
    let current = journal
        .state()
        .tools
        .get(KWEB_TOOL_INSTANCE)
        .cloned()
        .unwrap_or_default();
    let mut existing = Vec::with_capacity(current.slots.len());
    for slot in &current.slots {
        let state = journal
            .state()
            .box_state(slot.box_id)
            .ok_or_else(|| Error::new("Kweb tool slot box is missing"))?;
        existing.push(ExistingKwebSlot {
            box_id: slot.box_id,
            actual_slot: slot.slot.clone(),
            logical_slot: kweb_logical_slot(state, &slot.slot),
            retired: slot.retired,
            active: state.active,
            name: state.name.clone(),
            content: state.canonical.content.clone(),
        });
    }

    let mut desired_by_logical = BTreeMap::new();
    for (index, desired) in desired_nodes.iter().enumerate() {
        if desired_by_logical
            .insert(desired.logical_slot.clone(), index)
            .is_some()
        {
            return Err(Error::new(
                "Kweb box layout contains duplicate logical slots",
            ));
        }
    }

    let fresh_entries = connection_summary_entries(&fresh_connections.content)?;
    let mut fresh_by_id = HashMap::new();
    for entry in &fresh_entries {
        if fresh_by_id
            .insert(entry.id.clone(), entry.text.clone())
            .is_some()
        {
            return Err(Error::new(
                "Kweb connection projection contains duplicate IDs",
            ));
        }
    }

    let mut connection_boxes = BTreeMap::new();
    let mut represented_connection_ids = HashSet::new();
    let mut duplicate_connection_ids = HashSet::new();
    for (index, slot) in existing.iter().enumerate() {
        if kweb_role(&slot.content) != Some("connection-summary") {
            continue;
        }
        let heading = connection_summary_heading(&slot.content)?;
        let entries = connection_summary_entries(&slot.content)?;
        for entry in &entries {
            if !represented_connection_ids.insert(entry.id.clone()) {
                duplicate_connection_ids.insert(entry.id.clone());
            }
        }
        connection_boxes.insert(index, ExistingConnectionBox { heading, entries });
    }

    let mut slots = existing
        .iter()
        .map(|slot| ToolSlotInput {
            slot: slot.actual_slot.clone(),
            name: slot.name.clone(),
            content: slot.content.clone(),
            retired: slot.retired,
        })
        .collect::<Vec<_>>();
    let mut represented_full_nodes = HashSet::new();
    let mut selected_full_nodes = HashSet::new();
    let mut advanced_existing = Vec::new();

    for (index, old) in existing.iter().enumerate() {
        let mut advanced = false;
        if is_full_node_role(kweb_role(&old.content)) {
            represented_full_nodes.insert(old.logical_slot.clone());
            if old.active
                && !old.retired
                && desired_by_logical.contains_key(&old.logical_slot)
                && selected_full_nodes.insert(old.logical_slot.clone())
            {
                let desired = &desired_nodes[desired_by_logical[&old.logical_slot]];
                let mut content = desired.content.clone();
                preserve_metadata_key(&mut content, &old.content, "kwebLogicalSlot");
                preserve_metadata_key(&mut content, &old.content, "kwebRole");
                if content != old.content {
                    slots[index].content = content;
                    advanced = true;
                }
            }
        }

        if let Some(connection_box) = connection_boxes.get(&index) {
            let mut contains_duplicate = false;
            for entry in &connection_box.entries {
                if duplicate_connection_ids.contains(&entry.id) {
                    contains_duplicate = true;
                }
            }
            if old.active && !old.retired && !contains_duplicate {
                let mut entries = connection_box.entries.clone();
                let mut changed = false;
                for entry in &mut entries {
                    if let Some(text) = fresh_by_id.get(&entry.id)
                        && entry.text != *text
                    {
                        entry.text.clone_from(text);
                        changed = true;
                    }
                }
                if changed {
                    let mut content = old.content.clone();
                    content.text = format_connection_summary_entries_with_heading(
                        connection_box.heading,
                        &entries,
                    );
                    content.metadata["revisionHash"] = json!(revision_hash(&content.text));
                    if content != old.content {
                        slots[index].content = content;
                        advanced = true;
                    }
                }
            }
        }

        if advanced {
            advanced_existing.push(old.box_id);
        }
    }

    let mut used_actual_slots = existing
        .iter()
        .map(|slot| slot.actual_slot.clone())
        .collect::<HashSet<_>>();
    let mut used_logical_slots = existing
        .iter()
        .map(|slot| slot.logical_slot.clone())
        .collect::<HashSet<_>>();

    for desired in desired_nodes {
        if represented_full_nodes.contains(&desired.logical_slot) {
            continue;
        }
        let actual_slot = unique_slot(&desired.logical_slot, &mut used_actual_slots);
        used_logical_slots.insert(desired.logical_slot.clone());
        slots.push(ToolSlotInput {
            slot: actual_slot,
            name: desired.name,
            content: desired.content,
            retired: false,
        });
    }

    let additions = fresh_entries
        .iter()
        .filter(|entry| !represented_connection_ids.contains(&entry.id))
        .cloned()
        .collect::<Vec<_>>();
    let had_connection_box = !connection_boxes.is_empty();
    let mut next_addition = 0;
    while next_addition < additions.len()
        || (!had_connection_box && additions.is_empty() && next_addition == 0)
    {
        let end = (next_addition + CONNECTION_SUMMARIES_PER_BOX).min(additions.len());
        let entries = additions[next_addition..end].to_vec();
        let logical_slot = unique_connection_summary_logical_slot(&mut used_logical_slots);
        let actual_slot = unique_slot(&logical_slot, &mut used_actual_slots);
        let mut content = fresh_connections.content.clone();
        update_connection_summary_content(&mut content, &logical_slot, &entries);
        slots.push(ToolSlotInput {
            slot: actual_slot,
            name: fresh_connections.name.clone(),
            content,
            retired: false,
        });
        if additions.is_empty() {
            break;
        }
        next_addition = end;
    }

    Ok(CacheSafeLoadPlan {
        slots,
        advanced_existing,
    })
}

fn preserve_metadata_key(target: &mut BoxContent, source: &BoxContent, key: &str) {
    if let Some(value) = source.metadata.get(key) {
        target.metadata[key] = value.clone();
    } else if let Some(metadata) = target.metadata.as_object_mut() {
        metadata.remove(key);
    }
}

fn unique_connection_summary_logical_slot(used: &mut HashSet<String>) -> String {
    let mut sequence = 1_u64;
    loop {
        let candidate = if sequence == 1 {
            CONNECTION_SUMMARIES_LOGICAL_SLOT.to_owned()
        } else {
            format!("{CONNECTION_SUMMARIES_LOGICAL_SLOT}:{sequence}")
        };
        if used.insert(candidate.clone()) {
            return candidate;
        }
        sequence += 1;
    }
}

type KwebBoxVersions = BTreeMap<BoxId, (String, EventId)>;

fn kweb_box_versions(journal: &HistorySession) -> KwebBoxVersions {
    journal
        .state()
        .tool_layouts
        .get(KWEB_TOOL_INSTANCE)
        .into_iter()
        .flatten()
        .filter_map(|box_id| {
            let state = journal.state().box_state(*box_id)?;
            state
                .active
                .then(|| (*box_id, (state.name.clone(), state.canonical.event_id)))
        })
        .collect()
}

fn changed_kweb_box_ids(journal: &HistorySession, previous: &KwebBoxVersions) -> Vec<BoxId> {
    journal
        .state()
        .tool_layouts
        .get(KWEB_TOOL_INSTANCE)
        .into_iter()
        .flatten()
        .filter_map(|box_id| {
            let state = journal.state().box_state(*box_id)?;
            let current = (state.name.as_str(), state.canonical.event_id);
            let changed = previous
                .get(box_id)
                .map(|(name, revision)| (name.as_str(), *revision) != current)
                .unwrap_or(true);
            (state.active && changed).then_some(*box_id)
        })
        .collect()
}

fn unique_slot(logical: &str, used: &mut HashSet<String>) -> String {
    if used.insert(logical.to_owned()) {
        return logical.to_owned();
    }
    let mut generation = 2_u64;
    loop {
        let candidate = format!("{logical}#generation-{generation}");
        if used.insert(candidate.clone()) {
            return candidate;
        }
        generation += 1;
    }
}

fn reconcile_kweb_slots(
    journal: &mut HistorySession,
    recorded_at: &str,
    desired: Vec<DesiredKwebBox>,
) -> Result<()> {
    let current = journal
        .state()
        .tools
        .get(KWEB_TOOL_INSTANCE)
        .cloned()
        .unwrap_or_default();
    let desired_by_logical = desired
        .iter()
        .enumerate()
        .map(|(index, entry)| (entry.logical_slot.as_str(), index))
        .collect::<BTreeMap<_, _>>();
    if desired_by_logical.len() != desired.len() {
        return Err(Error::new(
            "Kweb box layout contains duplicate logical slots",
        ));
    }
    let mut claimed = HashSet::new();
    let mut actual_by_desired = BTreeMap::new();
    let mut slots = Vec::with_capacity(current.slots.len() + desired.len());
    let mut used_actual = current
        .slots
        .iter()
        .map(|slot| slot.slot.clone())
        .collect::<HashSet<_>>();
    for slot in &current.slots {
        let state = journal
            .state()
            .box_state(slot.box_id)
            .ok_or_else(|| Error::new("Kweb tool slot box is missing"))?;
        let logical = kweb_logical_slot(state, &slot.slot);
        let selected = !slot.retired
            && desired_by_logical.contains_key(logical.as_str())
            && claimed.insert(logical.clone());
        if selected {
            let entry = &desired[desired_by_logical[logical.as_str()]];
            slots.push(ToolSlotInput {
                slot: slot.slot.clone(),
                name: entry.name.clone(),
                content: entry.content.clone(),
                retired: false,
            });
            actual_by_desired.insert(entry.logical_slot.clone(), slot.slot.clone());
        } else {
            slots.push(ToolSlotInput {
                slot: slot.slot.clone(),
                name: state.name.clone(),
                content: state.canonical.content.clone(),
                retired: slot.retired || !selected,
            });
        }
    }
    for entry in &desired {
        if actual_by_desired.contains_key(&entry.logical_slot) {
            continue;
        }
        let actual = unique_slot(&entry.logical_slot, &mut used_actual);
        slots.push(ToolSlotInput {
            slot: actual.clone(),
            name: entry.name.clone(),
            content: entry.content.clone(),
            retired: false,
        });
        actual_by_desired.insert(entry.logical_slot.clone(), actual);
    }
    let layout_slots = desired
        .iter()
        .map(|entry| actual_by_desired[&entry.logical_slot].clone())
        .collect::<Vec<_>>();
    journal
        .apply_tool_slots_with_layout(recorded_at, KWEB_TOOL_INSTANCE, slots, &layout_slots)
        .map_err(|error| Error::new(format!("applying Kweb projection: {error}")))?;
    Ok(())
}

fn canonical_node_id(value: &str) -> Result<()> {
    value
        .parse::<NodeId>()
        .map(|_| ())
        .map_err(|_| Error::new(format!("{value:?} is not a canonical Kweb node ID")))
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;
    use std::time::{SystemTime, UNIX_EPOCH};

    use super::*;
    use kcode_session_history::{
        Config as HistoryConfig, NewSession, SessionHistory,
        chatend::{Representation, SessionKind},
    };
    use serde_json::json;

    fn id(index: u8) -> String {
        NodeId::from_bytes([0, 0, 0, 0, 0, index])
            .unwrap()
            .to_string()
    }

    fn connection(index: u8) -> Connection {
        Connection {
            id: id(index),
            short_name: format!("Node {index}"),
            short_description: format!("Summary {index}"),
        }
    }

    fn node(index: u8, fixed: &[u8], recent: &[u8]) -> Node {
        Node {
            id: id(index),
            short_name: format!("Node {index}"),
            short_description: format!("Summary {index}"),
            long_description: format!("Long description {index}"),
            owner: id(1),
            fixed_connections: fixed.iter().copied().map(connection).collect(),
            recent_connections: recent.iter().copied().map(connection).collect(),
            objects: vec![],
            last_modified_by: "test-model-high".into(),
            last_modified_at: Some("2026-07-28T00:00:00Z".into()),
        }
    }

    fn node_with_recent_description(
        index: u8,
        fixed: &[u8],
        recent: &[u8],
        description: &str,
    ) -> Node {
        let mut node = node(index, fixed, recent);
        for (connection, connection_index) in node.recent_connections.iter_mut().zip(recent) {
            connection.short_description = format!("{description} {connection_index}");
        }
        node
    }

    fn draft(index: u8, recent: &[u8]) -> NodeDraft {
        NodeDraft {
            short_name: format!("Node {index}"),
            short_description: format!("Summary {index}"),
            long_description: format!("Long description {index}"),
            owner: id(1),
            fixed_connections: Vec::new(),
            recent_connections: recent.iter().map(|value| id(*value)).collect(),
            objects: Vec::new(),
        }
    }

    fn test_journal(label: &str) -> (PathBuf, HistorySession) {
        let root = std::env::temp_dir().join(format!(
            "kcode-kweb-context-{label}-{}-{}",
            std::process::id(),
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        let history = SessionHistory::open(HistoryConfig {
            directory: root.join("sessions"),
            completed_list: root.join("completed.jsonl"),
            provider_cost_compatibility: None,
        })
        .unwrap();
        let journal = history
            .create_session(NewSession {
                kind: SessionKind::Conversation,
                created_at: "2026-07-29T00:00:00Z".into(),
                effective_context_tokens: 10_000,
                channel: Value::Null,
            })
            .unwrap();
        (root, journal)
    }

    fn connection_summary_box_ids(journal: &HistorySession) -> Vec<BoxId> {
        journal
            .state()
            .tool_layouts
            .get(KWEB_TOOL_INSTANCE)
            .into_iter()
            .flatten()
            .copied()
            .filter(|box_id| {
                journal
                    .state()
                    .box_state(*box_id)
                    .and_then(|state| state.canonical.content.metadata.get("kwebRole"))
                    .and_then(Value::as_str)
                    == Some("connection-summary")
            })
            .collect()
    }

    fn all_connection_summary_box_ids(journal: &HistorySession) -> Vec<BoxId> {
        journal
            .state()
            .tools
            .get(KWEB_TOOL_INSTANCE)
            .into_iter()
            .flat_map(|tool| &tool.slots)
            .filter_map(|slot| {
                let state = journal.state().box_state(slot.box_id)?;
                (kweb_role(&state.canonical.content) == Some("connection-summary"))
                    .then_some(slot.box_id)
            })
            .collect()
    }

    fn box_id_for_logical(journal: &HistorySession, logical: &str) -> BoxId {
        journal.state().tools[KWEB_TOOL_INSTANCE]
            .slots
            .iter()
            .find_map(|slot| {
                let state = journal.state().box_state(slot.box_id).unwrap();
                (kweb_logical_slot(state, &slot.slot) == logical).then_some(slot.box_id)
            })
            .unwrap()
    }

    fn tool_inputs_and_layout(journal: &HistorySession) -> (Vec<ToolSlotInput>, Vec<String>) {
        let tool = &journal.state().tools[KWEB_TOOL_INSTANCE];
        let inputs = tool
            .slots
            .iter()
            .map(|slot| {
                let state = journal.state().box_state(slot.box_id).unwrap();
                ToolSlotInput {
                    slot: slot.slot.clone(),
                    name: state.name.clone(),
                    content: state.canonical.content.clone(),
                    retired: slot.retired,
                }
            })
            .collect();
        let layout = journal.state().tool_layouts[KWEB_TOOL_INSTANCE]
            .iter()
            .map(|box_id| {
                tool.slots
                    .iter()
                    .find(|slot| slot.box_id == *box_id)
                    .unwrap()
                    .slot
                    .clone()
            })
            .collect();
        (inputs, layout)
    }

    fn metadata_without_revision(metadata: &Value) -> Value {
        let mut metadata = metadata.clone();
        metadata.as_object_mut().unwrap().remove("revisionHash");
        metadata
    }

    #[test]
    fn compatibility_fixed_nodes_yield_to_direct_loads() {
        let mut context = Context::with_fixed_connections(vec![id(1)], true).unwrap();
        context
            .apply_load(node(1, &[2], &[]), vec![node(2, &[], &[])])
            .unwrap();
        let report = context.apply_load(node(2, &[], &[]), Vec::new()).unwrap();
        assert!(report.promoted_from_fixed);
        assert_eq!(context.loaded_node_ids(), &[id(1), id(2)]);
        assert!(context.fixed_node_ids().is_empty());
        assert_eq!(
            context
                .box_specs(&BTreeMap::new(), &[])
                .unwrap()
                .iter()
                .map(|spec| spec.kind)
                .collect::<Vec<_>>(),
            vec![BoxKind::Loaded, BoxKind::Loaded, BoxKind::Connections]
        );
    }

    #[test]
    fn default_projection_keeps_only_direct_nodes_full() {
        let mut context = Context::new(vec![id(1)]).unwrap();
        context.apply_load(node(1, &[2], &[3]), Vec::new()).unwrap();

        let projected = context.projection(&BTreeMap::new(), &[]).unwrap();
        assert_eq!(
            projected
                .iter()
                .map(|item| item.key.clone())
                .collect::<Vec<_>>(),
            vec![
                id(1),
                format!("connection-summaries:{}", id(2)),
                format!("connection-summaries:{}", id(3)),
            ]
        );
        assert_eq!(projected[0].name, "Kweb loaded node");
        assert!(projected[0].text.contains("Long description 1"));
        assert_eq!(projected[1].name, "Kweb connection map marker");
        assert!(projected[1].text.contains(&id(2)));
        assert!(!projected[1].text.contains(&id(3)));
        assert_eq!(projected[2].name, "Kweb connection map marker");
        assert!(projected[2].text.contains(&id(3)));
        assert!(!projected[2].text.contains(&id(2)));
        assert!(!context.contains_full_node(&id(2)));
    }

    #[test]
    fn box_free_connection_states_change_independently() {
        let mut context = Context::new(vec![id(1)]).unwrap();
        context
            .apply_load(
                node_with_recent_description(1, &[], &[2, 3], "old"),
                Vec::new(),
            )
            .unwrap();
        let initial = context
            .projection(&BTreeMap::new(), &[])
            .unwrap()
            .into_iter()
            .map(|item| (item.key, item.text))
            .collect::<BTreeMap<_, _>>();

        context
            .refresh([node_with_recent_description(1, &[], &[2, 3, 4], "old")])
            .unwrap();
        let expanded = context
            .projection(&BTreeMap::new(), &[])
            .unwrap()
            .into_iter()
            .map(|item| (item.key, item.text))
            .collect::<BTreeMap<_, _>>();
        assert_eq!(
            expanded[&format!("connection-summaries:{}", id(2))],
            initial[&format!("connection-summaries:{}", id(2))]
        );
        assert_eq!(
            expanded[&format!("connection-summaries:{}", id(3))],
            initial[&format!("connection-summaries:{}", id(3))]
        );
        assert!(expanded.contains_key(&format!("connection-summaries:{}", id(4))));

        let mut changed_node = node_with_recent_description(1, &[], &[2, 3, 4], "old");
        changed_node.recent_connections[1].short_description = "changed 3".into();
        context.refresh([changed_node]).unwrap();
        let changed = context
            .projection(&BTreeMap::new(), &[])
            .unwrap()
            .into_iter()
            .map(|item| (item.key, item.text))
            .collect::<BTreeMap<_, _>>();
        let second = format!("connection-summaries:{}", id(2));
        let third = format!("connection-summaries:{}", id(3));
        let fourth = format!("connection-summaries:{}", id(4));
        assert_eq!(changed[&second], expanded[&second]);
        assert_ne!(changed[&third], expanded[&third]);
        assert_eq!(changed[&fourth], expanded[&fourth]);
        assert_eq!(changed[&id(1)], expanded[&id(1)]);
    }

    #[test]
    fn full_node_kinds_share_one_body_format_without_active_connections() {
        let mut context = Context::with_fixed_connections(vec![id(1)], true).unwrap();
        context
            .apply_load(node(1, &[2], &[]), vec![node(2, &[], &[])])
            .unwrap();
        let create = StagedCreate {
            pending_id: "pending:1".into(),
            data: draft(3, &[]),
        };
        let specs = context.box_specs(&BTreeMap::new(), &[create]).unwrap();
        assert_eq!(specs[0].kind.name(), "Kweb loaded node");
        assert_eq!(specs[1].kind.name(), "Kweb fixed connection");
        assert_eq!(specs[2].kind.name(), "Kweb staged node");
        for spec in &specs[..3] {
            assert!(spec.text.contains("Node ID:"));
            assert!(spec.text.contains("Node name:"));
            assert!(spec.text.contains("Node owner ID:"));
            assert!(spec.text.contains("Fixed connection IDs:"));
            assert!(spec.text.contains("Recent connection IDs:"));
            assert!(!spec.text.contains("Active"));
        }
        assert_eq!(
            specs[0].text,
            concat!(
                "Node ID: AAAAAAAB\n",
                "Node name: Node 1\n",
                "Map marker: Summary 1\n",
                "Node owner ID: AAAAAAAB\n",
                "Node long description:\n",
                "  Long description 1\n",
                "Fixed connection IDs: AAAAAAAC\n",
                "Recent connection IDs: none"
            )
        );
    }

    #[test]
    fn fixed_and_recent_connections_share_one_ordered_deduplicated_box() {
        let mut context = Context::new(vec![id(1)]).unwrap();
        context
            .apply_load(node(1, &[2], &[4, 5]), Vec::new())
            .unwrap();
        context
            .apply_load(node(2, &[8], &[5, 6, 7]), Vec::new())
            .unwrap();
        let creates = vec![StagedCreate {
            pending_id: "pending:1".into(),
            data: draft(3, &[6, 7]),
        }];
        let specs = context.box_specs(&BTreeMap::new(), &creates).unwrap();
        let connections = specs
            .iter()
            .filter(|spec| spec.kind == BoxKind::Connections)
            .collect::<Vec<_>>();
        assert_eq!(connections.len(), 1);
        assert_eq!(
            connections[0].text,
            format!(
                concat!(
                    "Connection map markers\n",
                    "{} · Node 2: Summary 2\n",
                    "{} · Node 4: Summary 4\n",
                    "{} · Node 5: Summary 5\n",
                    "{} · Node 8: Summary 8\n",
                    "{} · Node 6: Summary 6\n",
                    "{} · Node 7: Summary 7"
                ),
                id(2),
                id(4),
                id(5),
                id(8),
                id(6),
                id(7)
            )
        );
    }

    #[test]
    fn empty_connection_projection_is_still_one_exact_box() {
        let mut context = Context::new(vec![id(1)]).unwrap();
        context.apply_load(node(1, &[], &[]), Vec::new()).unwrap();
        let specs = context.box_specs(&BTreeMap::new(), &[]).unwrap();
        assert_eq!(specs.len(), 2);
        assert_eq!(specs[1].kind, BoxKind::Connections);
        assert_eq!(specs[1].text, "Connection map markers\nNone.");
    }

    #[test]
    fn connection_projection_rejects_missing_name_or_description() {
        for missing_name in [true, false] {
            let mut source = node(1, &[], &[2]);
            if missing_name {
                source.recent_connections[0].short_name.clear();
            } else {
                source.recent_connections[0].short_description.clear();
            }
            let mut context = Context::new(vec![id(1)]).unwrap();
            context.apply_load(source, Vec::new()).unwrap();
            assert_eq!(
                context
                    .box_specs(&BTreeMap::new(), &[])
                    .unwrap_err()
                    .to_string(),
                format!(
                    "connection map marker {} must resolve to a nonempty node name and map marker",
                    id(2)
                )
            );
        }
    }

    #[test]
    fn connection_summary_entries_accept_only_legacy_and_new_headings() {
        let entry = format!("{} · Node 2: Summary 2", id(2));
        for heading in ["Connection summaries", "Connection map markers"] {
            let content = BoxContent {
                text: format!("{heading}\n{entry}"),
                objects: Vec::new(),
                metadata: json!({CONNECTION_SUMMARY_IDS_METADATA: [id(2)]}),
            };
            assert_eq!(
                connection_summary_entries(&content).unwrap(),
                vec![ConnectionSummaryEntry {
                    id: id(2),
                    text: entry.clone(),
                }]
            );
        }

        for heading in [
            "Connection summary",
            "Connection map marker",
            "Connection map markers extra",
        ] {
            let content = BoxContent {
                text: format!("{heading}\n{entry}"),
                objects: Vec::new(),
                metadata: json!({}),
            };
            assert_eq!(
                connection_summary_entries(&content)
                    .unwrap_err()
                    .to_string(),
                "Kweb connection-summary box has an invalid heading"
            );
        }

        let content = BoxContent {
            text: "Connection map markers".into(),
            objects: Vec::new(),
            metadata: json!({}),
        };
        assert_eq!(
            connection_summary_entries(&content)
                .unwrap_err()
                .to_string(),
            "Kweb connection-summary box has no body"
        );
    }

    #[test]
    fn fresh_connection_marker_rendering_uses_only_new_terminology() {
        let mut context = Context::new(vec![id(1)]).unwrap();
        context.apply_load(node(1, &[2], &[]), Vec::new()).unwrap();

        let specs = context.box_specs(&BTreeMap::new(), &[]).unwrap();
        let markers = specs.last().unwrap();
        assert_eq!(markers.kind.name(), "Kweb connection map markers");
        assert!(markers.text.starts_with("Connection map markers\n"));
        assert!(!markers.text.contains("Connection summaries"));

        let projected = context.projection(&BTreeMap::new(), &[]).unwrap();
        assert_eq!(projected[1].name, "Kweb connection map marker");
        assert!(!projected[1].name.contains("summary"));
    }

    #[test]
    fn staged_updates_drive_full_text_and_recent_projection() {
        let mut context = Context::new(vec![id(1)]).unwrap();
        context.apply_load(node(1, &[3], &[2]), Vec::new()).unwrap();
        let mut updates = BTreeMap::new();
        updates.insert(id(1), draft(9, &[3]));
        let specs = context.box_specs(&updates, &[]).unwrap();
        assert!(specs[0].text.contains("Node name: Node 9"));
        assert!(specs[0].staged_node.is_some());
        assert!(!specs.last().unwrap().text.contains(&id(2)));
        assert!(specs.last().unwrap().text.contains(&id(3)));
    }

    #[test]
    fn sync_appends_fresh_connection_boxes_eight_at_a_time() {
        let (root, mut journal) = test_journal("connection-boxes");
        let mut context = Context::new(vec![id(1)]).unwrap();
        let initial_indices = (2..=19).collect::<Vec<_>>();
        context
            .apply_load(
                node_with_recent_description(1, &[], &initial_indices, "old"),
                Vec::new(),
            )
            .unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();

        let original_ids = connection_summary_box_ids(&journal);
        assert_eq!(
            original_ids
                .iter()
                .map(|box_id| {
                    connection_summary_entries(
                        &journal
                            .state()
                            .box_state(*box_id)
                            .unwrap()
                            .canonical
                            .content,
                    )
                    .unwrap()
                    .len()
                })
                .collect::<Vec<_>>(),
            vec![8, 8, 2]
        );
        let original_revisions = original_ids
            .iter()
            .map(|box_id| {
                journal
                    .state()
                    .box_state(*box_id)
                    .unwrap()
                    .canonical
                    .event_id
            })
            .collect::<Vec<_>>();
        journal
            .summarize_box("t2", original_ids[0], "retained first box")
            .unwrap();
        journal.dehydrate_boxes("t3", &original_ids[1..=2]).unwrap();

        let expanded_indices = (2..=28).collect::<Vec<_>>();
        context
            .refresh([node_with_recent_description(
                1,
                &[],
                &expanded_indices,
                "new",
            )])
            .unwrap();
        let changed = context
            .sync_chatend(&mut journal, "t4", &BTreeMap::new(), &[])
            .unwrap();
        let current_ids = connection_summary_box_ids(&journal);
        assert_eq!(
            current_ids
                .iter()
                .map(|box_id| {
                    connection_summary_entries(
                        &journal
                            .state()
                            .box_state(*box_id)
                            .unwrap()
                            .canonical
                            .content,
                    )
                    .unwrap()
                    .len()
                })
                .collect::<Vec<_>>(),
            vec![8, 8, 2, 8, 1]
        );
        assert_eq!(&current_ids[..3], original_ids.as_slice());
        assert!(changed.contains(&original_ids[0]));
        assert!(changed.contains(&original_ids[1]));
        assert!(changed.contains(&original_ids[2]));
        assert!(changed.contains(&current_ids[3]));
        assert!(changed.contains(&current_ids[4]));

        let first = journal.state().box_state(original_ids[0]).unwrap();
        assert_ne!(first.canonical.event_id, original_revisions[0]);
        assert!(first.canonical.content.text.contains("new 2"));
        assert!(matches!(
            first.representation,
            Representation::Summarized { based_on, .. } if based_on == original_revisions[0]
        ));
        let second = journal.state().box_state(original_ids[1]).unwrap();
        assert_ne!(second.canonical.event_id, original_revisions[1]);
        assert!(second.canonical.content.text.contains("new 10"));
        assert!(matches!(
            second.representation,
            Representation::Dehydrated { based_on } if based_on == original_revisions[1]
        ));
        let third = journal.state().box_state(original_ids[2]).unwrap();
        assert_ne!(third.canonical.event_id, original_revisions[2]);
        assert!(!third.canonical.content.text.contains("new 25"));
        assert!(third.canonical.content.text.contains("new 19"));
        assert!(matches!(
            third.representation,
            Representation::Dehydrated { based_on } if based_on == original_revisions[2]
        ));
        let fourth = journal.state().box_state(current_ids[3]).unwrap();
        assert!(fourth.canonical.content.text.contains("new 20"));
        assert!(fourth.canonical.content.text.contains("new 27"));
        assert!(!fourth.canonical.content.text.contains("new 28"));
        let fifth = journal.state().box_state(current_ids[4]).unwrap();
        assert!(fifth.canonical.content.text.contains("new 28"));

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn ordinary_sync_upgrades_legacy_text_without_membership_or_state_changes() {
        let (root, mut journal) = test_journal("legacy-heading-upgrade");
        let mut context = Context::new(vec![id(1)]).unwrap();
        let indices = (2..=10).collect::<Vec<_>>();
        context
            .apply_load(node(1, &[], &indices), Vec::new())
            .unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();

        let original_ids = connection_summary_box_ids(&journal);
        assert_eq!(original_ids.len(), 2);
        let current_tool = journal.state().tools[KWEB_TOOL_INSTANCE].clone();
        let layout_slots = journal.state().tool_layouts[KWEB_TOOL_INSTANCE]
            .iter()
            .map(|box_id| {
                current_tool
                    .slots
                    .iter()
                    .find(|slot| slot.box_id == *box_id)
                    .unwrap()
                    .slot
                    .clone()
            })
            .collect::<Vec<_>>();
        let legacy_slots = current_tool
            .slots
            .iter()
            .map(|slot| {
                let state = journal.state().box_state(slot.box_id).unwrap();
                let mut content = state.canonical.content.clone();
                let mut name = state.name.clone();
                if content.metadata.get("kwebRole").and_then(Value::as_str)
                    == Some("connection-summary")
                {
                    content.text =
                        content
                            .text
                            .replacen("Connection map markers", "Connection summaries", 1);
                    content.metadata["revisionHash"] = json!(revision_hash(&content.text));
                    content.metadata["retainedCompatibilityMetadata"] =
                        json!(content.metadata["kwebLogicalSlot"].clone());
                    name = "Kweb connection summaries".into();
                }
                ToolSlotInput {
                    slot: slot.slot.clone(),
                    name,
                    content,
                    retired: slot.retired,
                }
            })
            .collect::<Vec<_>>();
        journal
            .apply_tool_slots_with_layout("t2", KWEB_TOOL_INSTANCE, legacy_slots, &layout_slots)
            .unwrap();

        let legacy = original_ids
            .iter()
            .map(|box_id| {
                let slot = journal.state().tools[KWEB_TOOL_INSTANCE]
                    .slots
                    .iter()
                    .find(|slot| slot.box_id == *box_id)
                    .unwrap()
                    .slot
                    .clone();
                let state = journal.state().box_state(*box_id).unwrap();
                assert_eq!(state.name, "Kweb connection summaries");
                assert!(
                    state
                        .canonical
                        .content
                        .text
                        .starts_with("Connection summaries\n")
                );
                (
                    slot,
                    state.canonical.event_id,
                    connection_summary_entries(&state.canonical.content)
                        .unwrap()
                        .into_iter()
                        .map(|entry| entry.id)
                        .collect::<Vec<_>>(),
                    metadata_without_revision(&state.canonical.content.metadata),
                )
            })
            .collect::<Vec<_>>();
        journal
            .summarize_box("t3", original_ids[0], "retained legacy map markers")
            .unwrap();
        journal.dehydrate_boxes("t4", &original_ids[1..]).unwrap();

        let changed = context
            .sync_chatend(&mut journal, "t5", &BTreeMap::new(), &[])
            .unwrap();
        assert_eq!(connection_summary_box_ids(&journal), original_ids);
        assert!(changed.contains(&original_ids[0]));
        assert!(changed.contains(&original_ids[1]));

        for (index, box_id) in original_ids.iter().enumerate() {
            let state = journal.state().box_state(*box_id).unwrap();
            let actual_slot = journal.state().tools[KWEB_TOOL_INSTANCE]
                .slots
                .iter()
                .find(|slot| slot.box_id == *box_id)
                .unwrap()
                .slot
                .as_str();
            assert_eq!(actual_slot, legacy[index].0);
            assert_eq!(state.name, "Kweb connection map markers");
            assert!(
                state
                    .canonical
                    .content
                    .text
                    .starts_with("Connection map markers\n")
            );
            assert!(
                !state
                    .canonical
                    .content
                    .text
                    .contains("Connection summaries")
            );
            assert_eq!(
                connection_summary_entries(&state.canonical.content)
                    .unwrap()
                    .into_iter()
                    .map(|entry| entry.id)
                    .collect::<Vec<_>>(),
                legacy[index].2
            );
            assert_eq!(
                metadata_without_revision(&state.canonical.content.metadata),
                legacy[index].3
            );
            assert_eq!(
                state.canonical.content.metadata["revisionHash"],
                json!(revision_hash(&state.canonical.content.text))
            );
            if index == 0 {
                assert!(matches!(
                    state.representation,
                    Representation::Summarized { based_on, .. }
                        if based_on == legacy[index].1
                ));
            } else {
                assert!(matches!(
                    state.representation,
                    Representation::Dehydrated { based_on }
                        if based_on == legacy[index].1
                ));
            }
        }

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn sync_preserves_empty_connection_box_when_later_summaries_arrive() {
        let (root, mut journal) = test_journal("empty-connection-box");
        let mut context = Context::new(vec![id(1)]).unwrap();
        context.apply_load(node(1, &[], &[]), Vec::new()).unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();

        let original_boxes = connection_summary_box_ids(&journal);
        assert_eq!(original_boxes.len(), 1);
        let original_revision = journal
            .state()
            .box_state(original_boxes[0])
            .unwrap()
            .canonical
            .event_id;
        let content = &journal
            .state()
            .box_state(original_boxes[0])
            .unwrap()
            .canonical
            .content;
        assert!(connection_summary_entries(content).unwrap().is_empty());
        assert_eq!(content.metadata[CONNECTION_SUMMARY_IDS_METADATA], json!([]));

        context.refresh([node(1, &[], &[2])]).unwrap();
        let changed = context
            .sync_chatend(&mut journal, "t2", &BTreeMap::new(), &[])
            .unwrap();
        let current_boxes = connection_summary_box_ids(&journal);
        assert_eq!(current_boxes.len(), 2);
        assert_eq!(current_boxes[0], original_boxes[0]);
        assert!(!changed.contains(&original_boxes[0]));
        assert!(changed.contains(&current_boxes[1]));
        let original = journal.state().box_state(original_boxes[0]).unwrap();
        assert_eq!(original.canonical.event_id, original_revision);
        assert!(
            connection_summary_entries(&original.canonical.content)
                .unwrap()
                .is_empty()
        );
        let fresh = journal.state().box_state(current_boxes[1]).unwrap();
        assert_eq!(
            connection_summary_entries(&fresh.canonical.content)
                .unwrap()
                .iter()
                .map(|entry| entry.id.clone())
                .collect::<Vec<_>>(),
            vec![id(2)]
        );

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn sync_reports_only_changed_boxes_in_projection_order() {
        let (root, mut journal) = test_journal("changed-boxes");
        let mut context = Context::new(vec![id(1)]).unwrap();
        context.apply_load(node(1, &[], &[]), Vec::new()).unwrap();
        let initial = context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();
        assert_eq!(initial.len(), 2);
        assert!(
            context
                .sync_chatend(&mut journal, "t2", &BTreeMap::new(), &[],)
                .unwrap()
                .is_empty()
        );

        context.apply_load(node(1, &[2], &[]), Vec::new()).unwrap();
        let changed = context
            .sync_chatend(&mut journal, "t3", &BTreeMap::new(), &[])
            .unwrap();
        assert_eq!(changed.len(), 2);
        assert_eq!(
            changed
                .iter()
                .map(|box_id| journal.state().box_state(*box_id).unwrap().name.as_str())
                .collect::<Vec<_>>(),
            vec!["Kweb loaded node", "Kweb connection map markers"]
        );
        assert!(
            context
                .sync_chatend(&mut journal, "t4", &BTreeMap::new(), &[])
                .unwrap()
                .is_empty()
        );

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn load_sync_appends_unseen_nodes_after_exact_old_prefix_without_layout_change() {
        let (root, mut journal) = test_journal("load-node-tail");
        let mut context = Context::new(vec![id(1)]).unwrap();
        context.apply_load(node(1, &[], &[]), Vec::new()).unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();
        let layout = journal.state().tool_layouts[KWEB_TOOL_INSTANCE].clone();
        let old_slots = journal.state().tools[KWEB_TOOL_INSTANCE]
            .slots
            .iter()
            .map(|slot| {
                let state = journal.state().box_state(slot.box_id).unwrap();
                (
                    slot.slot.clone(),
                    slot.box_id,
                    slot.retired,
                    state.name.clone(),
                    state.canonical.content.clone(),
                    state.canonical.event_id,
                )
            })
            .collect::<Vec<_>>();

        context.apply_load(node(2, &[], &[]), Vec::new()).unwrap();
        let stale = context
            .sync_load_chatend(&mut journal, "t2", &BTreeMap::new(), &[])
            .unwrap();
        assert!(stale.is_empty());
        assert_eq!(journal.state().tool_layouts[KWEB_TOOL_INSTANCE], layout);
        let slots = &journal.state().tools[KWEB_TOOL_INSTANCE].slots;
        assert_eq!(slots.len(), old_slots.len() + 1);
        for (slot, old) in slots.iter().zip(&old_slots) {
            let state = journal.state().box_state(slot.box_id).unwrap();
            assert_eq!(slot.slot, old.0);
            assert_eq!(slot.box_id, old.1);
            assert_eq!(slot.retired, old.2);
            assert_eq!(state.name, old.3);
            assert_eq!(state.canonical.content, old.4);
            assert_eq!(state.canonical.event_id, old.5);
        }
        let appended = journal
            .state()
            .box_state(slots.last().unwrap().box_id)
            .unwrap();
        assert_eq!(
            appended.canonical.content.metadata["kwebLogicalSlot"],
            json!(id(2))
        );

        drop(journal);
        SessionHistory::open(HistoryConfig {
            directory: root.join("sessions"),
            completed_list: root.join("completed.jsonl"),
            provider_cost_compatibility: None,
        })
        .unwrap();
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn load_sync_never_fills_old_partial_or_empty_connection_boxes() {
        for (label, initial) in [("empty", Vec::new()), ("partial", vec![2])] {
            let (root, mut journal) = test_journal(label);
            let mut context = Context::new(vec![id(1)]).unwrap();
            context
                .apply_load(node(1, &[], &initial), Vec::new())
                .unwrap();
            context
                .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
                .unwrap();
            let old_box = all_connection_summary_box_ids(&journal)[0];
            let old_content = journal
                .state()
                .box_state(old_box)
                .unwrap()
                .canonical
                .content
                .clone();
            let old_event = journal
                .state()
                .box_state(old_box)
                .unwrap()
                .canonical
                .event_id;
            let layout = journal.state().tool_layouts[KWEB_TOOL_INSTANCE].clone();

            let expanded = (2..=19).collect::<Vec<_>>();
            context.refresh([node(1, &[], &expanded)]).unwrap();
            context
                .sync_load_chatend(&mut journal, "t2", &BTreeMap::new(), &[])
                .unwrap();
            let old = journal.state().box_state(old_box).unwrap();
            assert_eq!(old.canonical.content, old_content);
            assert_eq!(old.canonical.event_id, old_event);
            assert_eq!(journal.state().tool_layouts[KWEB_TOOL_INSTANCE], layout);
            let fresh = all_connection_summary_box_ids(&journal)
                .into_iter()
                .filter(|box_id| *box_id != old_box)
                .collect::<Vec<_>>();
            let expected_lengths = if initial.is_empty() {
                vec![8, 8, 2]
            } else {
                vec![8, 8, 1]
            };
            assert_eq!(
                fresh
                    .iter()
                    .map(|box_id| connection_summary_entries(
                        &journal
                            .state()
                            .box_state(*box_id)
                            .unwrap()
                            .canonical
                            .content
                    )
                    .unwrap()
                    .len())
                    .collect::<Vec<_>>(),
                expected_lengths
            );

            drop(journal);
            std::fs::remove_dir_all(root).unwrap();
        }
    }

    #[test]
    fn load_sync_metadata_only_advance_preserves_visible_and_occurrence_state() {
        let (root, mut journal) = test_journal("metadata-only");
        let mut context = Context::new(vec![id(1)]).unwrap();
        context.apply_load(node(1, &[], &[]), Vec::new()).unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();
        let full = box_id_for_logical(&journal, &id(1));
        let old = journal.state().box_state(full).unwrap();
        let old_name = old.name.clone();
        let old_text = old.canonical.content.text.clone();
        let old_metadata = old.canonical.content.metadata.clone();
        let old_event = old.canonical.event_id;
        let old_representation = old.representation.clone();
        let old_occurrences = old.occurrence_events.clone();
        let layout = journal.state().tool_layouts[KWEB_TOOL_INSTANCE].clone();

        let mut refreshed = node(1, &[], &[]);
        refreshed.last_modified_at = Some("2026-08-16T00:00:00Z".into());
        context.refresh([refreshed]).unwrap();
        assert_eq!(
            context
                .sync_load_chatend(&mut journal, "t2", &BTreeMap::new(), &[])
                .unwrap(),
            vec![full]
        );
        let current = journal.state().box_state(full).unwrap();
        assert_eq!(current.name, old_name);
        assert!(current.active);
        assert_eq!(current.canonical.content.text, old_text);
        assert_ne!(current.canonical.content.metadata, old_metadata);
        assert_ne!(current.canonical.event_id, old_event);
        assert_eq!(current.representation, old_representation);
        assert_eq!(current.occurrence_events, old_occurrences);
        assert_eq!(journal.state().tool_layouts[KWEB_TOOL_INSTANCE], layout);
        let current_event = current.canonical.event_id;
        assert!(
            context
                .sync_load_chatend(&mut journal, "t3", &BTreeMap::new(), &[])
                .unwrap()
                .is_empty()
        );
        assert_eq!(
            journal.state().box_state(full).unwrap().canonical.event_id,
            current_event
        );

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn load_sync_advances_full_and_marker_caches_once_without_visible_changes() {
        let (root, mut journal) = test_journal("cache-preservation");
        let mut context = Context::new(vec![id(1)]).unwrap();
        let indices = (2..=10).collect::<Vec<_>>();
        context
            .apply_load(
                node_with_recent_description(1, &[], &indices, "old"),
                Vec::new(),
            )
            .unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();
        let full = box_id_for_logical(&journal, &id(1));
        let connections = all_connection_summary_box_ids(&journal);
        journal
            .summarize_box("t2", full, "visible full summary")
            .unwrap();
        journal.dehydrate_boxes("t3", &connections[..1]).unwrap();
        let ordered = std::iter::once(full)
            .chain(connections.iter().copied())
            .collect::<Vec<_>>();
        let snapshots = ordered
            .iter()
            .map(|box_id| {
                let state = journal.state().box_state(*box_id).unwrap();
                (
                    state.name.clone(),
                    state.active,
                    state.canonical.event_id,
                    state.representation.clone(),
                    state.occurrence_events.clone(),
                )
            })
            .collect::<Vec<_>>();
        let layout = journal.state().tool_layouts[KWEB_TOOL_INSTANCE].clone();

        context
            .refresh([node_with_recent_description(1, &[], &indices, "new")])
            .unwrap();
        assert_eq!(
            context
                .sync_load_chatend(&mut journal, "t4", &BTreeMap::new(), &[])
                .unwrap(),
            ordered
        );
        for (index, box_id) in ordered.iter().enumerate() {
            let state = journal.state().box_state(*box_id).unwrap();
            assert_eq!(state.name, snapshots[index].0);
            assert_eq!(state.active, snapshots[index].1);
            assert_ne!(state.canonical.event_id, snapshots[index].2);
            assert_eq!(state.representation, snapshots[index].3);
            assert_eq!(state.occurrence_events, snapshots[index].4);
        }
        assert_eq!(journal.state().tool_layouts[KWEB_TOOL_INSTANCE], layout);
        let first_entries = connection_summary_entries(
            &journal
                .state()
                .box_state(connections[0])
                .unwrap()
                .canonical
                .content,
        )
        .unwrap();
        assert_eq!(first_entries.len(), 8);
        assert!(first_entries.iter().all(|entry| entry.text.contains("new")));
        assert!(
            context
                .sync_load_chatend(&mut journal, "t5", &BTreeMap::new(), &[])
                .unwrap()
                .is_empty()
        );

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn load_sync_preserves_legacy_connection_name_heading_and_metadata() {
        let (root, mut journal) = test_journal("load-legacy");
        let mut context = Context::new(vec![id(1)]).unwrap();
        context
            .apply_load(
                node_with_recent_description(1, &[], &[2, 3], "old"),
                Vec::new(),
            )
            .unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();
        let connection_box = all_connection_summary_box_ids(&journal)[0];
        let (mut inputs, layout_slots) = tool_inputs_and_layout(&journal);
        for input in &mut inputs {
            if input.slot
                == journal.state().tools[KWEB_TOOL_INSTANCE]
                    .slots
                    .iter()
                    .find(|slot| slot.box_id == connection_box)
                    .unwrap()
                    .slot
            {
                input.name = "Kweb connection summaries".into();
                input.content.text = input.content.text.replacen(
                    "Connection map markers",
                    "Connection summaries",
                    1,
                );
                input.content.metadata["revisionHash"] = json!(revision_hash(&input.content.text));
                input.content.metadata["legacyMetadata"] = json!({"kept": true});
            }
        }
        journal
            .apply_tool_slots_with_layout("t2", KWEB_TOOL_INSTANCE, inputs, &layout_slots)
            .unwrap();
        let old = journal.state().box_state(connection_box).unwrap();
        let old_metadata = metadata_without_revision(&old.canonical.content.metadata);
        let old_representation = old.representation.clone();
        let old_occurrences = old.occurrence_events.clone();
        let layout = journal.state().tool_layouts[KWEB_TOOL_INSTANCE].clone();

        context
            .refresh([node_with_recent_description(1, &[], &[2, 3], "new")])
            .unwrap();
        let stale = context
            .sync_load_chatend(&mut journal, "t3", &BTreeMap::new(), &[])
            .unwrap();
        assert!(stale.contains(&connection_box));
        let current = journal.state().box_state(connection_box).unwrap();
        assert_eq!(current.name, "Kweb connection summaries");
        assert!(
            current
                .canonical
                .content
                .text
                .starts_with("Connection summaries\n")
        );
        assert!(
            !current
                .canonical
                .content
                .text
                .contains("Connection map markers")
        );
        assert_eq!(
            metadata_without_revision(&current.canonical.content.metadata),
            old_metadata
        );
        assert_eq!(current.representation, old_representation);
        assert_eq!(current.occurrence_events, old_occurrences);
        assert_eq!(journal.state().tool_layouts[KWEB_TOOL_INSTANCE], layout);
        assert!(
            connection_summary_entries(&current.canonical.content)
                .unwrap()
                .iter()
                .all(|entry| entry.text.contains("new"))
        );

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn load_sync_keeps_fixed_identity_on_promotion_and_never_retires_shrunk_closure() {
        let (root, mut journal) = test_journal("fixed-promotion");
        let mut context = Context::with_fixed_connections(vec![id(1)], true).unwrap();
        context
            .apply_load(
                node(1, &[2, 3], &[]),
                vec![node(2, &[], &[]), node(3, &[], &[])],
            )
            .unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();
        let promoted_box = box_id_for_logical(&journal, &id(2));
        let removed_box = box_id_for_logical(&journal, &id(3));
        let promoted_slot = journal.state().tools[KWEB_TOOL_INSTANCE]
            .slots
            .iter()
            .find(|slot| slot.box_id == promoted_box)
            .unwrap()
            .slot
            .clone();
        let removed_event = journal
            .state()
            .box_state(removed_box)
            .unwrap()
            .canonical
            .event_id;
        let layout = journal.state().tool_layouts[KWEB_TOOL_INSTANCE].clone();

        let mut promoted = node(2, &[], &[]);
        promoted.long_description = "Promoted direct content".into();
        context.apply_load(promoted, Vec::new()).unwrap();
        context.refresh([node(1, &[], &[])]).unwrap();
        let stale = context
            .sync_load_chatend(&mut journal, "t2", &BTreeMap::new(), &[])
            .unwrap();
        assert!(stale.contains(&promoted_box));
        let promoted = journal.state().box_state(promoted_box).unwrap();
        assert_eq!(promoted.name, "Kweb fixed connection");
        assert_eq!(
            promoted.canonical.content.metadata["kwebRole"],
            json!("fixed")
        );
        assert!(
            promoted
                .canonical
                .content
                .text
                .contains("Promoted direct content")
        );
        let promoted_current_slot = journal.state().tools[KWEB_TOOL_INSTANCE]
            .slots
            .iter()
            .find(|slot| slot.box_id == promoted_box)
            .unwrap();
        assert_eq!(promoted_current_slot.slot, promoted_slot);
        assert!(!promoted_current_slot.retired);
        let removed = journal.state().box_state(removed_box).unwrap();
        assert!(removed.active);
        assert_eq!(removed.canonical.event_id, removed_event);
        assert!(
            !journal.state().tools[KWEB_TOOL_INSTANCE]
                .slots
                .iter()
                .find(|slot| slot.box_id == removed_box)
                .unwrap()
                .retired
        );
        assert_eq!(journal.state().tool_layouts[KWEB_TOOL_INSTANCE], layout);

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }

    #[test]
    fn load_sync_global_dedupe_counts_duplicate_and_retired_connection_boxes() {
        let (root, mut journal) = test_journal("global-dedupe");
        let mut context = Context::new(vec![id(1)]).unwrap();
        let initial = (2..=18).collect::<Vec<_>>();
        context
            .apply_load(
                node_with_recent_description(1, &[], &initial, "old"),
                Vec::new(),
            )
            .unwrap();
        context
            .sync_chatend(&mut journal, "t1", &BTreeMap::new(), &[])
            .unwrap();
        let old_boxes = all_connection_summary_box_ids(&journal);
        assert_eq!(old_boxes.len(), 3);
        let duplicate_entry = connection_summary_entries(
            &journal
                .state()
                .box_state(old_boxes[0])
                .unwrap()
                .canonical
                .content,
        )
        .unwrap()[0]
            .clone();
        let (mut inputs, mut layout_slots) = tool_inputs_and_layout(&journal);
        let tool = &journal.state().tools[KWEB_TOOL_INSTANCE];
        let second_slot = tool
            .slots
            .iter()
            .find(|slot| slot.box_id == old_boxes[1])
            .unwrap()
            .slot
            .clone();
        let retired_slot = tool
            .slots
            .iter()
            .find(|slot| slot.box_id == old_boxes[2])
            .unwrap()
            .slot
            .clone();
        for input in &mut inputs {
            if input.slot == second_slot {
                let mut entries = connection_summary_entries(&input.content).unwrap();
                entries[0] = duplicate_entry.clone();
                let logical = input.content.metadata["kwebLogicalSlot"]
                    .as_str()
                    .unwrap()
                    .to_owned();
                update_connection_summary_content(&mut input.content, &logical, &entries);
            }
            if input.slot == retired_slot {
                input.retired = true;
            }
        }
        layout_slots.retain(|slot| slot != &retired_slot);
        journal
            .apply_tool_slots_with_layout("t2", KWEB_TOOL_INSTANCE, inputs, &layout_slots)
            .unwrap();
        let old_events = old_boxes
            .iter()
            .map(|box_id| {
                journal
                    .state()
                    .box_state(*box_id)
                    .unwrap()
                    .canonical
                    .event_id
            })
            .collect::<Vec<_>>();
        let layout = journal.state().tool_layouts[KWEB_TOOL_INSTANCE].clone();

        let expanded = (2..=20).collect::<Vec<_>>();
        context
            .refresh([node_with_recent_description(1, &[], &expanded, "new")])
            .unwrap();
        let stale = context
            .sync_load_chatend(&mut journal, "t3", &BTreeMap::new(), &[])
            .unwrap();
        for (index, box_id) in old_boxes.iter().enumerate() {
            assert!(!stale.contains(box_id));
            assert_eq!(
                journal
                    .state()
                    .box_state(*box_id)
                    .unwrap()
                    .canonical
                    .event_id,
                old_events[index]
            );
        }
        assert_eq!(journal.state().tool_layouts[KWEB_TOOL_INSTANCE], layout);
        let fresh = all_connection_summary_box_ids(&journal)
            .into_iter()
            .filter(|box_id| !old_boxes.contains(box_id))
            .collect::<Vec<_>>();
        assert_eq!(fresh.len(), 1);
        assert_eq!(
            connection_summary_entries(
                &journal
                    .state()
                    .box_state(fresh[0])
                    .unwrap()
                    .canonical
                    .content
            )
            .unwrap()
            .into_iter()
            .map(|entry| entry.id)
            .collect::<Vec<_>>(),
            vec![id(10), id(19), id(20)]
        );
        assert!(
            journal.state().tools[KWEB_TOOL_INSTANCE]
                .slots
                .iter()
                .find(|slot| slot.box_id == old_boxes[2])
                .unwrap()
                .retired
        );

        drop(journal);
        std::fs::remove_dir_all(root).unwrap();
    }
}