diffler 0.14.1

Terminal code review for AI coding agents
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
//! App-side handling of agent tool calls. Runs synchronously on the main
//! loop against the owned review state; the `mcp` module only ships
//! requests here and renders the responses.

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

use diffler_core::model::DiffModel;
use diffler_core::session::{Anchor, Comment, CommentStatus, now_unix};
use diffler_core::source::ReviewSource;
use diffler_core::walkthrough::{
    BODY_MAX_BYTES, MAX_STOPS, Receipt, ReceiptCode, TOTAL_MAX_BYTES, Target, Walkthrough,
};

use super::App;
use crate::mcp::{
    AGENT_AUTHOR, CommentInfo, FileEntry, McpRequestKind, McpResponse, NoteInfo, NoteParams,
    ReceiptInfo, ReviewStatusResponse, ReviewSummary, StopInfo, StopParams, WalkthroughInfo,
    WalkthroughPublished, WalkthroughSummary, comment_info, comment_status_name, file_status_name,
    render_unified,
};

/// The [`ReviewSource`] variants whose diffs are computed once and cached;
/// `WorkingTree` and `Against` always read live and never reach
/// [`App::source_model`]'s cache path.
enum CachedKind<'a> {
    Commit(&'a str),
    Range(&'a str, &'a str),
    Pr(u64),
}

impl App {
    pub(crate) fn handle_mcp(&mut self, kind: McpRequestKind) -> McpResponse {
        match kind {
            McpRequestKind::ReviewStatus => McpResponse::Status(self.review_status_response()),
            McpRequestKind::GetDiff { file } => {
                match render_unified(self.review.model(), file.as_deref()) {
                    Ok(diff) => McpResponse::Diff(diff),
                    Err(message) => McpResponse::Error(message),
                }
            }
            McpRequestKind::GetComments { status } => McpResponse::Comments(
                self.comments_response(|c| status.is_none_or(|wanted| c == wanted)),
            ),
            McpRequestKind::ListReviews => McpResponse::Reviews(self.review_summaries()),
            McpRequestKind::ReplyComment { id, body } => self.agent_reply(&id, &body),
            McpRequestKind::ProposeResolve { id, note } => {
                self.agent_propose_resolve(&id, note.as_deref())
            }
            McpRequestKind::MarkViewed { file } => self.agent_mark_viewed(&file),
            McpRequestKind::Feedback => McpResponse::Feedback {
                comments: self.comments_response(|c| c != CommentStatus::Resolved),
            },
            McpRequestKind::PublishWalkthrough {
                id,
                title,
                stops,
                skipped,
                summary,
            } => self.agent_publish_walkthrough(id, title, &stops, skipped, summary),
            McpRequestKind::GetWalkthrough { id } => match self.walkthrough_info(id.as_deref()) {
                Ok(info) => McpResponse::Walkthrough(info),
                Err(err) => McpResponse::Error(err),
            },
        }
    }

    fn review_status_response(&self) -> ReviewStatusResponse {
        let files_changed = self
            .review
            .model()
            .files
            .iter()
            .map(|f| FileEntry {
                path: f.path.clone(),
                status: file_status_name(f.status).to_owned(),
                viewed: self.review.session.is_viewed(&f.path, &f.content_hash()),
            })
            .collect();
        let (open, replied, resolved) = count_by_status(&self.review.session.comments);
        // named, not just skipped, so an agent knows a walkthrough or review
        // might be missing from the lists below because its file would not
        // parse, rather than reading a clean repository that has none
        let corrupt_reviews = self
            .review
            .all_reviews_and_corrupt()
            .map_or_else(|_| Vec::new(), |(_, corrupt)| corrupt)
            .iter()
            .filter_map(|path| path.file_name())
            .map(|name| name.to_string_lossy().into_owned())
            .collect();
        ReviewStatusResponse {
            repo: self
                .review
                .repo_root
                .file_name()
                .map(|name| name.to_string_lossy().into_owned())
                .unwrap_or_default(),
            branch: self.head.branch.clone(),
            oid7: self.head.oid7.clone(),
            files_changed,
            open_comments: open,
            replied_comments: replied,
            resolved_comments: resolved,
            feedback_epoch: self.feedback_epoch(),
            reviews: self.review_summaries(),
            walkthroughs: self.walkthrough_summaries(),
            corrupt_reviews,
        }
    }

    /// Every walkthrough on disk, newest published first.
    fn walkthrough_summaries(&self) -> Vec<WalkthroughSummary> {
        let mut rows: Vec<WalkthroughSummary> = self
            .review
            .all_reviews()
            .unwrap_or_default()
            .into_iter()
            .filter_map(|(source, session)| {
                let ReviewSource::Walkthrough { id } = source else {
                    return None;
                };
                let walkthrough = session.walkthrough?;
                Some(WalkthroughSummary {
                    id,
                    title: walkthrough.title,
                    stops: walkthrough.stops.len(),
                    at: walkthrough.at,
                })
            })
            .collect();
        rows.sort_by_key(|w| std::cmp::Reverse(w.at));
        rows
    }

    /// The walkthrough `id` names, or the newest one on disk when `id` is
    /// `None`. `Ok(None)` means no such walkthrough exists; a genuine read
    /// failure (a corrupt review file) is `Err`, so the two are never folded
    /// into the same answer.
    fn walkthrough_info(&mut self, id: Option<&str>) -> Result<Option<WalkthroughInfo>, String> {
        let id = match id {
            Some(id) => id.to_owned(),
            None => match self.walkthrough_summaries().into_iter().next() {
                Some(summary) => summary.id,
                None => return Ok(None),
            },
        };
        let source = ReviewSource::Walkthrough { id };
        self.review
            .ensure_source(&source)
            .map_err(|err| err.to_string())?;
        let session = self.review.session_for(&source);
        let Some(walkthrough) = session.walkthrough.as_ref() else {
            return Ok(None);
        };
        Ok(Some(WalkthroughInfo {
            id: walkthrough.id.clone(),
            title: walkthrough.title.clone(),
            author: walkthrough.author.clone(),
            at: walkthrough.at,
            skipped: walkthrough.skipped.clone(),
            summary: walkthrough.summary.clone(),
            rev: walkthrough.rev.clone(),
            stops: walkthrough
                .stops
                .iter()
                .zip(walkthrough.notes_by_stop(&session.comments))
                .filter_map(|(id, notes)| {
                    let comment = session.comment(id)?;
                    Some(StopInfo {
                        id: comment.id.clone(),
                        title: comment.title.clone().unwrap_or_default(),
                        anchor: comment.anchor_ref.clone(),
                        body: comment.body.clone(),
                        notes: notes
                            .iter()
                            .filter_map(|id| {
                                let note = session.comment(id)?;
                                Some(NoteInfo {
                                    id: note.id.clone(),
                                    anchor: note.anchor_ref.clone(),
                                    body: note.body.clone(),
                                })
                            })
                            .collect(),
                    })
                })
                .collect(),
        }))
    }

    /// Store the agent's reading order as its own review source: revises the
    /// walkthrough `id` names, or creates a new one when `id` is `None`.
    /// Every stop becomes an agent comment, so the human answers it in its
    /// own thread; a stop that passes its comment id back keeps that thread,
    /// and every other agent comment the source held goes with it (a human
    /// comment or reply is never one of these, so it always survives).
    /// Anchor and figure receipts are reported but never refuse: the
    /// walkthrough is stored either way, so the agent learns what to fix
    /// without a broken figure ever reaching the reader.
    fn agent_publish_walkthrough(
        &mut self,
        id: Option<String>,
        title: String,
        stops: &[StopParams],
        skipped: Option<String>,
        summary: Option<String>,
    ) -> McpResponse {
        let id = id.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
        let source = ReviewSource::Walkthrough { id: id.clone() };
        if let Err(err) = self.review.ensure_source(&source) {
            return McpResponse::Error(err.to_string());
        }
        // every publish, a revision included, redescribes the stops against
        // whatever is checked out right now, so it is pinned to that HEAD
        // and not whatever an earlier revision of this same walkthrough
        // named; a repo with no commits yet resolves to nothing, the same as
        // a walkthrough saved before `rev` existed
        let rev = self.review.vcs.resolve("HEAD").ok();
        // a stop with no anchor still needs a file to hang its card on: the
        // first one the walkthrough names, else the first file of the
        // working tree, which a walkthrough always tracks
        let model = self.review.model().clone();
        let fallback = stops
            .iter()
            .filter_map(|stop| stop.anchor.as_deref())
            .map(|anchor| Target::parse(anchor).path().to_owned())
            .next()
            .or_else(|| model.files.first().map(|file| file.path.clone()))
            .unwrap_or_default();
        let files: Vec<String> = stops
            .iter()
            .map(|stop| anchored_path(stop.anchor.as_deref(), &fallback))
            .collect();

        let refusals = walkthrough_refusals(stops, &files, summary.as_deref());
        if !refusals.is_empty() {
            let text = refusals
                .iter()
                .map(|receipt| {
                    let stop = receipt
                        .stop
                        .map_or("-".to_owned(), |index| index.to_string());
                    format!("stop {stop}: {}: {}", receipt.code.name(), receipt.detail)
                })
                .collect::<Vec<_>>()
                .join("\n");
            return McpResponse::Error(text);
        }

        let receipts = walkthrough_receipts(stops);
        let kept: HashSet<&str> = stops
            .iter()
            .flat_map(|stop| {
                std::iter::once(stop.id.as_deref())
                    .chain(notes_of(stop).map(|note| note.id.as_deref()))
            })
            .flatten()
            .collect();
        let session = self.review.session_for_mut(&source);
        // every agent comment this source already held is this walkthrough's
        // previous revision; a fresh source has none, so nothing to prune
        let previous: Vec<String> = session
            .comments
            .iter()
            .filter(|c| c.author == AGENT_AUTHOR)
            .map(|c| c.id.clone())
            .collect();
        for id in &previous {
            if !kept.contains(id.as_str()) {
                session.delete_comment(id);
            }
        }

        let mut stop_ids = Vec::with_capacity(stops.len());
        for (stop, file) in stops.iter().zip(&files) {
            let id = write_agent_comment(
                session,
                stop.id.as_deref(),
                Some(stop.title.clone()),
                stop.anchor.clone(),
                file,
                &stop.body,
            );
            stop_ids.push(id);
            for note in notes_of(stop) {
                // a note with no anchor of its own rides the stop's, and the
                // worker seats it where that region starts
                let anchor = note.anchor.clone().or_else(|| stop.anchor.clone());
                write_agent_comment(session, note.id.as_deref(), None, anchor, file, &note.body);
            }
        }

        let count = stop_ids.len();
        session.set_walkthrough(Walkthrough {
            id: id.clone(),
            title,
            author: AGENT_AUTHOR.to_owned(),
            at: diffler_core::session::now_unix_millis(),
            stops: stop_ids,
            skipped,
            summary,
            rev: rev.clone(),
        });
        if let Err(err) = self.persist_review_change(&source) {
            return McpResponse::Error(format!("walkthrough not saved: {err}"));
        }
        self.ensure_walkthrough_view();
        self.queue_walkthrough_anchors();
        self.reload_walkthroughs();
        self.info("agent published a walkthrough");

        McpResponse::WalkthroughPublished(WalkthroughPublished {
            id,
            stops: count,
            receipts,
            rev,
        })
    }

    fn review_summaries(&self) -> Vec<ReviewSummary> {
        self.review
            .all_reviews()
            .unwrap_or_default()
            .into_iter()
            .map(|(source, session)| {
                let (open, replied, resolved) = count_by_status(&session.comments);
                // a walkthrough's own title reads better than its fallback
                // `walkthrough <id8>` label, once the session is loaded
                let label = session
                    .walkthrough
                    .as_ref()
                    .map_or_else(|| source.label(), |w| w.title.clone());
                ReviewSummary {
                    source: source.key(),
                    label,
                    open_comments: open,
                    replied_comments: replied,
                    resolved_comments: resolved,
                }
            })
            .collect()
    }

    /// The diff a source is reviewing, used to render comment context and judge
    /// outdated-ness. Commit and range diffs are immutable, so they compute
    /// once and stay cached: agent polls must not stall the render loop.
    /// Backend errors degrade to an empty diff.
    pub(crate) fn source_model(&mut self, source: &ReviewSource) -> std::sync::Arc<DiffModel> {
        // narrowing to the cacheable sources up front makes WorkingTree
        // structurally absent below, instead of an unreachable match arm
        let kind = match source {
            // a walkthrough always tracks the working tree, the same as
            // `WorkingTree` itself, never pinned to a rev
            ReviewSource::WorkingTree | ReviewSource::Walkthrough { .. } => {
                return std::sync::Arc::new(self.review.model().clone());
            }
            // live like the working tree, so caching it would go stale; the
            // open view already holds a model the refresh keeps current
            ReviewSource::Against { rev } => {
                return std::sync::Arc::new(self.against_model_for(rev));
            }
            ReviewSource::Commit { oid } => CachedKind::Commit(oid),
            ReviewSource::Range { oldest, newest } => CachedKind::Range(oldest, newest),
            ReviewSource::Pr { number } => CachedKind::Pr(*number),
        };
        let key = source.key();
        if !self.source_models.contains_key(&key) {
            let model = match kind {
                CachedKind::Commit(oid) => self.review.vcs.commit_diff(oid).unwrap_or_default(),
                CachedKind::Range(oldest, newest) => self
                    .review
                    .vcs
                    .range_diff(oldest, newest)
                    .unwrap_or_default(),
                // resolved when the PR view opened; unknown PRs degrade empty
                CachedKind::Pr(number) => self
                    .pr_ranges
                    .get(&number)
                    .and_then(|(base, head)| self.review.vcs.tree_diff(base, head).ok())
                    .unwrap_or_default(),
            };
            self.source_models
                .insert(key.clone(), std::sync::Arc::new(model));
        }
        self.source_models.get(&key).cloned().unwrap_or_default()
    }

    /// Comments across every review, each tagged with its source so the agent
    /// knows what the human reviewed and where the change came from.
    fn comments_response(&mut self, keep: impl Fn(CommentStatus) -> bool) -> Vec<CommentInfo> {
        let mut out = Vec::new();
        for (source, session) in self.review.all_reviews().unwrap_or_default() {
            let comments: Vec<_> = session.comments.iter().filter(|c| keep(c.status)).collect();
            // a live source rebuilds its model here, so an agent poll must not
            // pay for one whose comments it is about to discard
            if comments.is_empty() {
                continue;
            }
            let model = self.source_model(&source);
            for comment in comments {
                out.push(comment_info(comment, &model, &source));
            }
        }
        out
    }

    /// The review a comment id lives in, searching every persisted source.
    fn source_of_comment(&self, id: &str) -> Option<ReviewSource> {
        self.review
            .all_reviews()
            .ok()?
            .into_iter()
            .find(|(_, session)| session.comments.iter().any(|c| c.id == id))
            .map(|(source, _)| source)
    }

    fn agent_reply(&mut self, id: &str, body: &str) -> McpResponse {
        let Some(source) = self.source_of_comment(id) else {
            return McpResponse::Error(format!("unknown comment id: {id}"));
        };
        if let Err(err) = self.review.ensure_source(&source) {
            return McpResponse::Error(err.to_string());
        }
        self.review
            .session_for_mut(&source)
            .reply(id, AGENT_AUTHOR, body);
        if let Err(err) = self.persist_review_change(&source) {
            return McpResponse::Error(err);
        }
        self.info("agent replied to a comment");
        self.comment_status_response(&source, id)
    }

    /// The agent can only propose: the comment moves to replied and the human
    /// resolves it in the TUI (`R`). The note lands as the reply when the
    /// thread is empty, so a flag on an answered comment adds nothing.
    fn agent_propose_resolve(&mut self, id: &str, note: Option<&str>) -> McpResponse {
        let Some(source) = self.source_of_comment(id) else {
            return McpResponse::Error(format!("unknown comment id: {id}"));
        };
        if let Err(err) = self.review.ensure_source(&source) {
            return McpResponse::Error(err.to_string());
        }
        let session = self.review.session_for_mut(&source);
        // the agent's own answer is what a note would restate; a reply from
        // the human or another reviewer says nothing about this flag
        let answered = session.comment(id).is_some_and(|comment| {
            comment
                .replies
                .iter()
                .any(|reply| reply.author == AGENT_AUTHOR)
        });
        // the note speaks only when the thread is otherwise empty: an agent
        // that replied and then proposed would say the same thing twice
        match note.map(str::trim).filter(|note| !note.is_empty()) {
            Some(note) if !answered => {
                session.reply(id, AGENT_AUTHOR, note);
            }
            _ => {
                session.mark_replied(id);
            }
        }
        if let Err(err) = self.persist_review_change(&source) {
            return McpResponse::Error(err);
        }
        self.info("agent proposed resolving a comment (confirm with R)");
        self.comment_status_response(&source, id)
    }

    fn comment_status_response(&self, source: &ReviewSource, id: &str) -> McpResponse {
        let status = self
            .review
            .session_for(source)
            .comments
            .iter()
            .find(|c| c.id == id)
            .map_or(CommentStatus::Open, |c| c.status);
        McpResponse::Replied {
            status: comment_status_name(status).to_owned(),
        }
    }

    fn agent_mark_viewed(&mut self, file: &str) -> McpResponse {
        // mark the file in the review the human is currently looking at, so a
        // commit/range diff gets its own viewed marks like the working tree
        let source = self.active_review_source();
        let Some(hash) = self
            .source_model(&source)
            .files
            .iter()
            .find(|f| f.path == file)
            .map(diffler_core::model::FileDiff::content_hash)
        else {
            return McpResponse::Error(format!("unknown file: {file}"));
        };
        if let Err(err) = self.review.ensure_source(&source) {
            return McpResponse::Error(err.to_string());
        }
        self.review
            .session_for_mut(&source)
            .mark_viewed(file, &hash);
        if let Err(err) = self.persist_review_change(&source) {
            return McpResponse::Error(err);
        }
        self.info(format!("agent marked {file} viewed"));
        McpResponse::Ok
    }
}

/// The file an anchor names, falling back to the file a walkthrough hangs its
/// anchorless cards on.
fn anchored_path(anchor: Option<&str>, fallback: &str) -> String {
    anchor.map_or_else(
        || fallback.to_owned(),
        |anchor| Target::parse(anchor).path().to_owned(),
    )
}

fn notes_of(stop: &StopParams) -> impl Iterator<Item = &NoteParams> {
    stop.notes.iter().flatten()
}

/// Write one comment the walkthrough owns, reusing the one `id` names so a
/// revision keeps the thread hanging off it. Lines stay unset: the worker
/// resolves `anchor_ref` against the file and fills them in.
fn write_agent_comment(
    session: &mut diffler_core::session::Session,
    id: Option<&str>,
    title: Option<String>,
    anchor_ref: Option<String>,
    file: &str,
    body: &str,
) -> String {
    let anchor = Anchor {
        file: anchored_path(anchor_ref.as_deref(), file),
        line: None,
        line_end: None,
        on_old_side: false,
        line_text: None,
    };
    let existing = id.and_then(|id| session.comments.iter_mut().find(|c| c.id == id));
    if let Some(comment) = existing {
        comment.title = title;
        comment.anchor_ref = anchor_ref;
        comment.anchor = anchor;
        body.clone_into(&mut comment.body);
        return comment.id.clone();
    }
    let id = uuid::Uuid::new_v4().to_string();
    session.comments.push(Comment {
        id: id.clone(),
        author: AGENT_AUTHOR.to_owned(),
        remote_id: None,
        thread_id: None,
        anchor,
        title,
        anchor_ref,
        body: body.to_owned(),
        status: CommentStatus::Open,
        replies: Vec::new(),
        at: now_unix(),
    });
    id
}

/// Every id repeated across `stops` and their notes within one publish. A
/// second write sharing an id with an earlier one in the same call would
/// find the comment the first just made and overwrite its title, anchor and
/// body, so the walkthrough ends up with a duplicate id in its stop list and
/// the first stop or note silently lost.
fn duplicate_id_receipts(stops: &[StopParams]) -> Vec<Receipt> {
    let mut first_seen: HashMap<&str, usize> = HashMap::new();
    let mut receipts = Vec::new();
    for (index, stop) in stops.iter().enumerate() {
        let ids = std::iter::once(stop.id.as_deref())
            .chain(notes_of(stop).map(|note| note.id.as_deref()))
            .flatten();
        for id in ids {
            if let Some(&first) = first_seen.get(id) {
                receipts.push(Receipt {
                    stop: Some(index),
                    code: ReceiptCode::DuplicateId,
                    detail: format!("id \"{id}\" repeats stop {first}'s"),
                });
            } else {
                first_seen.insert(id, index);
            }
        }
    }
    receipts
}

/// Every hard-limit receipt the incoming stops and summary earn. All codes
/// here are refusals: nothing is stored while any are present. `files` is the
/// file each stop lands in, which is the file its notes have to stay inside.
fn walkthrough_refusals(
    stops: &[StopParams],
    files: &[String],
    summary: Option<&str>,
) -> Vec<Receipt> {
    let mut receipts = Vec::new();
    if stops.is_empty() {
        receipts.push(Receipt {
            stop: None,
            code: ReceiptCode::EmptyStops,
            detail: "a walkthrough needs at least one stop".to_owned(),
        });
    } else if stops.len() > MAX_STOPS {
        receipts.push(Receipt {
            stop: None,
            code: ReceiptCode::TooManyStops,
            detail: format!("{} stops, {MAX_STOPS} at most", stops.len()),
        });
    }
    receipts.extend(duplicate_id_receipts(stops));

    let mut total = summary.map_or(0, str::len);
    if let Some(summary) = summary.filter(|summary| summary.len() > BODY_MAX_BYTES) {
        receipts.push(Receipt {
            stop: None,
            code: ReceiptCode::BodyTooLong,
            detail: format!("summary: {} bytes, {BODY_MAX_BYTES} at most", summary.len()),
        });
    }
    for (index, stop) in stops.iter().enumerate() {
        total += stop.body.len();
        for (at, note) in notes_of(stop).enumerate() {
            total += note.body.len();
            if note.body.len() > BODY_MAX_BYTES {
                receipts.push(Receipt {
                    stop: Some(index),
                    code: ReceiptCode::BodyTooLong,
                    detail: format!(
                        "note {at}: {} bytes, {BODY_MAX_BYTES} at most",
                        note.body.len()
                    ),
                });
            }
            let Some(anchor) = note.anchor.as_deref() else {
                continue;
            };
            let path = Target::parse(anchor).path().to_owned();
            if files.get(index).is_some_and(|file| *file != path) {
                receipts.push(Receipt {
                    stop: Some(index),
                    code: ReceiptCode::NoteOutsideStop,
                    detail: format!(
                        "note {at} anchors \"{anchor}\", outside stop {index}'s file {}",
                        files.get(index).map_or("", String::as_str)
                    ),
                });
            }
        }
        if stop.body.len() > BODY_MAX_BYTES {
            receipts.push(Receipt {
                stop: Some(index),
                code: ReceiptCode::BodyTooLong,
                detail: format!("{} bytes, {BODY_MAX_BYTES} at most", stop.body.len()),
            });
        }
        // `Target::parse` never fails, so the only unparsable anchor is one
        // with nothing in it
        if stop.anchor.as_deref().is_some_and(|a| a.trim().is_empty()) {
            receipts.push(Receipt {
                stop: Some(index),
                code: ReceiptCode::AnchorUnparsed,
                detail: "anchor is empty".to_owned(),
            });
        }
    }
    if total > TOTAL_MAX_BYTES {
        receipts.push(Receipt {
            stop: None,
            code: ReceiptCode::TotalTooLong,
            detail: format!("{total} bytes total, {TOTAL_MAX_BYTES} at most"),
        });
    }
    receipts
}

fn count_by_status(comments: &[diffler_core::session::Comment]) -> (usize, usize, usize) {
    let (mut open, mut replied, mut resolved) = (0, 0, 0);
    for comment in comments {
        match comment.status {
            CommentStatus::Open => open += 1,
            CommentStatus::Replied => replied += 1,
            CommentStatus::Resolved => resolved += 1,
        }
    }
    (open, replied, resolved)
}

/// Receipts for a walkthrough that already cleared `validate`: a bare-path
/// anchor highlights the whole file rather than a span, and a stop's own
/// `mermaid` fences may have drawn simplified or, with no shape at all,
/// dropped to their source. Reported so the agent learns without a broken
/// figure ever reaching the reader.
fn walkthrough_receipts(stops: &[StopParams]) -> Vec<ReceiptInfo> {
    let mut receipts = Vec::new();
    for (index, stop) in stops.iter().enumerate() {
        if let Some(anchor) = &stop.anchor
            && matches!(Target::parse(anchor), Target::File { .. })
        {
            receipts.push(ReceiptInfo {
                stop: Some(index),
                code: "anchor_whole".to_owned(),
                detail: format!("\"{anchor}\" has no symbol or line; the whole file anchors"),
            });
        }
        let bodies = std::iter::once(&stop.body).chain(notes_of(stop).map(|note| &note.body));
        for body in bodies {
            let (figures, notes) = crate::app::walkthrough::validate(body);
            let code = if figures == 0 {
                "figure_dropped"
            } else {
                "figure_simplified"
            };
            receipts.extend(notes.into_iter().map(|detail| ReceiptInfo {
                stop: Some(index),
                code: code.to_owned(),
                detail,
            }));
        }
    }
    receipts
}

#[cfg(test)]
mod tests {
    use diffler_core::session::Anchor;

    use super::*;
    use crate::config::LoadedConfig;
    use crate::test_support::standard_fixture;

    fn app_with_comment() -> (crate::test_support::Fixture, App, String) {
        let fixture = standard_fixture();
        let mut app = App::new(fixture.review(), LoadedConfig::default());
        let id = app
            .review
            .session
            .add_comment(
                Anchor {
                    file: "src/lib.rs".to_owned(),
                    line: Some(2),
                    line_end: None,
                    on_old_side: false,
                    line_text: Some("    42".to_owned()),
                },
                "human",
                "why 42?",
            )
            .id
            .clone();
        (fixture, app, id)
    }

    /// A live source rebuilds its diff whenever the agent asks for comments, so
    /// one contributing nothing must not be built at all. The cache is the
    /// observable: a commit source populates it the moment its model is built.
    #[test]
    fn a_source_contributing_no_comments_builds_no_model() {
        let fixture = standard_fixture();
        let mut app = App::new(fixture.review(), LoadedConfig::default());
        let oid = app.status.recent[0].oid.clone();
        app.review
            .ensure_source(&ReviewSource::commit(&oid))
            .expect("source");

        assert!(app.comments_response(|_| true).is_empty());
        assert!(
            app.source_models.is_empty(),
            "a commentless source was diffed anyway"
        );

        // and it is built as soon as that source has something to say
        app.review
            .session_for_mut(&ReviewSource::commit(&oid))
            .add_comment(
                Anchor {
                    file: "src/lib.rs".to_owned(),
                    line: Some(2),
                    line_end: None,
                    on_old_side: false,
                    line_text: Some("    42".to_owned()),
                },
                "human",
                "why 42?",
            );
        assert_eq!(app.comments_response(|_| true).len(), 1);
        assert!(
            app.source_models
                .contains_key(&ReviewSource::commit(&oid).key()),
            "the commented source was diffed"
        );
    }

    #[test]
    fn commit_models_are_computed_once_and_cached() {
        let fixture = standard_fixture();
        let mut app = App::new(fixture.review(), LoadedConfig::default());
        let oid = app.status.recent[0].oid.clone();
        let source = ReviewSource::commit(&oid);
        let first = app.source_model(&source);
        let second = app.source_model(&source);
        assert!(
            std::sync::Arc::ptr_eq(&first, &second),
            "second lookup reuses the cached model"
        );
    }

    #[test]
    fn review_status_reports_files_counts_and_epoch() {
        let (fixture, mut app, _id) = app_with_comment();
        let McpResponse::Status(status) = app.handle_mcp(McpRequestKind::ReviewStatus) else {
            panic!("expected a status response");
        };
        assert_eq!(
            status.repo,
            fixture.root.file_name().unwrap().to_string_lossy()
        );
        assert_eq!(status.branch.as_deref(), Some("main"));
        assert_eq!(status.oid7.len(), 7);
        assert!(status.files_changed.iter().any(|f| f.path == "src/lib.rs"));
        assert!(status.files_changed.iter().all(|f| !f.viewed));
        assert_eq!(status.open_comments, 1);
        assert_eq!(status.replied_comments, 0);
        assert_eq!(status.resolved_comments, 0);
        assert_eq!(status.feedback_epoch, 0);
    }

    #[test]
    fn get_diff_renders_and_rejects_unknown_files() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::Diff(diff) = app.handle_mcp(McpRequestKind::GetDiff { file: None }) else {
            panic!("expected a diff response");
        };
        assert!(diff.contains("+++ b/src/lib.rs"));
        assert!(diff.contains("+    42"));

        let response = app.handle_mcp(McpRequestKind::GetDiff {
            file: Some("nope.rs".to_owned()),
        });
        assert!(matches!(response, McpResponse::Error(message) if message.contains("nope.rs")));
    }

    #[test]
    fn pairing_deferral_does_not_change_mcp_diff_or_feedback() {
        let (_fixture, mut app, _id) = app_with_comment();
        // capture the agent-facing outputs while the model carries no emphasis
        let McpResponse::Diff(before_diff) = app.handle_mcp(McpRequestKind::GetDiff { file: None })
        else {
            panic!("expected a diff response");
        };
        let McpResponse::Feedback {
            comments: before_feedback,
            ..
        } = app.handle_mcp(McpRequestKind::Feedback)
        else {
            panic!("expected comments");
        };

        // enrich the whole working model with intra-line emphasis, the thing
        // the backend used to do eagerly and the TUI now does per file
        for file in &mut app.review.model_mut().files {
            diffler_core::pairing::enrich_file(file);
        }
        let has_emphasis = app
            .review
            .model()
            .files
            .iter()
            .flat_map(|f| &f.hunks)
            .flat_map(|h| &h.lines)
            .any(|l| !l.emphasis.is_empty());
        assert!(has_emphasis, "the fixture has a paired line to emphasize");

        let McpResponse::Diff(after_diff) = app.handle_mcp(McpRequestKind::GetDiff { file: None })
        else {
            panic!("expected a diff response");
        };
        let McpResponse::Feedback {
            comments: after_feedback,
            ..
        } = app.handle_mcp(McpRequestKind::Feedback)
        else {
            panic!("expected comments");
        };
        // emphasis is a render-only concern: MCP output is byte-identical
        assert_eq!(before_diff, after_diff, "get_diff ignores emphasis");
        assert_eq!(before_feedback, after_feedback, "feedback ignores emphasis");
    }

    #[test]
    fn get_comments_filters_by_status() {
        let (_fixture, mut app, id) = app_with_comment();
        let McpResponse::Comments(all) =
            app.handle_mcp(McpRequestKind::GetComments { status: None })
        else {
            panic!("expected comments");
        };
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].id, id);
        assert_eq!(all[0].context.as_deref(), Some("-    41\n+    42\n }"));
        assert!(!all[0].outdated);

        let McpResponse::Comments(resolved) = app.handle_mcp(McpRequestKind::GetComments {
            status: Some(CommentStatus::Resolved),
        }) else {
            panic!("expected comments");
        };
        assert!(resolved.is_empty());
    }

    #[test]
    fn agent_reply_flips_status_persists_and_toasts() {
        let (fixture, mut app, id) = app_with_comment();
        let response = app.handle_mcp(McpRequestKind::ReplyComment {
            id: id.clone(),
            body: "it is the answer".to_owned(),
        });
        assert_eq!(
            response,
            McpResponse::Replied {
                status: "replied".to_owned()
            }
        );
        let comment = &app.review.session.comments[0];
        assert_eq!(comment.status, CommentStatus::Replied);
        assert_eq!(comment.replies[0].author, AGENT_AUTHOR);
        let message = app.message.clone().expect("toast");
        assert!(message.text.contains("agent replied"));
        // the agent's own mutation must not wake its feedback poll
        assert_eq!(app.feedback_epoch(), 0);
        let reloaded = diffler_core::store::load(&fixture.root).unwrap();
        assert_eq!(reloaded.comments[0].status, CommentStatus::Replied);
    }

    #[test]
    fn agent_reply_to_unknown_id_errors() {
        let (_fixture, mut app, _id) = app_with_comment();
        let response = app.handle_mcp(McpRequestKind::ReplyComment {
            id: "nope".to_owned(),
            body: "hello".to_owned(),
        });
        assert!(matches!(response, McpResponse::Error(message) if message.contains("nope")));
    }

    #[test]
    fn propose_resolve_on_an_empty_thread_leaves_the_note_and_stays_replied() {
        let (_fixture, mut app, id) = app_with_comment();
        let response = app.handle_mcp(McpRequestKind::ProposeResolve {
            id,
            note: Some("fixed in abc123".to_owned()),
        });
        assert_eq!(
            response,
            McpResponse::Replied {
                status: "replied".to_owned()
            }
        );
        let comment = &app.review.session.comments[0];
        assert_eq!(comment.status, CommentStatus::Replied, "not resolved");
        assert_eq!(
            comment
                .replies
                .iter()
                .map(|r| r.body.as_str())
                .collect::<Vec<_>>(),
            vec!["fixed in abc123"],
            "a bare flag still leaves its reasoning"
        );
    }

    /// The agent answers, then flags. The flag is a status change, so the card
    /// carries one reply rather than the answer plus a summary of it.
    #[test]
    fn propose_resolve_after_a_reply_writes_nothing() {
        let (_fixture, mut app, id) = app_with_comment();
        app.handle_mcp(McpRequestKind::ReplyComment {
            id: id.clone(),
            body: "checked the job: the host is right".to_owned(),
        });

        app.handle_mcp(McpRequestKind::ProposeResolve {
            id: id.clone(),
            note: Some("host and idProperty confirmed".to_owned()),
        });
        app.handle_mcp(McpRequestKind::ProposeResolve { id, note: None });

        let comment = &app.review.session.comments[0];
        assert_eq!(
            comment
                .replies
                .iter()
                .map(|r| r.body.as_str())
                .collect::<Vec<_>>(),
            vec!["checked the job: the host is right"],
            "the answer is the only thing in the thread"
        );
        assert_eq!(comment.status, CommentStatus::Replied);
    }

    #[test]
    fn propose_resolve_unknown_id_errors_without_touching_the_session() {
        let (_fixture, mut app, _id) = app_with_comment();
        let response = app.handle_mcp(McpRequestKind::ProposeResolve {
            id: "nope".to_owned(),
            note: Some("done".to_owned()),
        });
        assert!(matches!(response, McpResponse::Error(message) if message.contains("nope")));
        let comment = &app.review.session.comments[0];
        assert_eq!(comment.status, CommentStatus::Open);
        assert!(comment.replies.is_empty());
    }

    #[test]
    fn mark_viewed_reflects_in_review_status() {
        let (_fixture, mut app, _id) = app_with_comment();
        let response = app.handle_mcp(McpRequestKind::MarkViewed {
            file: "src/lib.rs".to_owned(),
        });
        assert_eq!(response, McpResponse::Ok);
        assert!(app.is_path_viewed("src/lib.rs"));

        let response = app.handle_mcp(McpRequestKind::MarkViewed {
            file: "nope.rs".to_owned(),
        });
        assert!(matches!(response, McpResponse::Error(_)));
    }

    fn commit_anchor(file: &str) -> Anchor {
        Anchor {
            file: file.to_owned(),
            line: Some(1),
            line_end: None,
            on_old_side: false,
            line_text: None,
        }
    }

    #[test]
    fn get_comments_aggregates_every_source_and_tags_provenance() {
        let (_fixture, mut app, working_id) = app_with_comment();
        let oid = app.status.recent[0].oid.clone();
        let source = ReviewSource::commit(&oid);
        app.review.ensure_source(&source).expect("ensure");
        let commit_id = app
            .review
            .session_for_mut(&source)
            .add_comment(commit_anchor("src/lib.rs"), "human", "on the commit")
            .id
            .clone();
        app.review.save_for(&source).expect("save");

        let McpResponse::Comments(all) =
            app.handle_mcp(McpRequestKind::GetComments { status: None })
        else {
            panic!("expected comments");
        };
        let working = all.iter().find(|c| c.id == working_id).expect("working");
        assert_eq!(working.source, "working");
        assert_eq!(working.source_label, "working tree");
        let on_commit = all.iter().find(|c| c.id == commit_id).expect("commit");
        assert_eq!(on_commit.source, source.key());
        assert_eq!(on_commit.source_label, source.label());
    }

    #[test]
    fn agent_reply_targets_the_comment_owning_source() {
        let (fixture, mut app, _working_id) = app_with_comment();
        let oid = app.status.recent[0].oid.clone();
        let source = ReviewSource::commit(&oid);
        app.review.ensure_source(&source).expect("ensure");
        let id = app
            .review
            .session_for_mut(&source)
            .add_comment(commit_anchor("src/lib.rs"), "human", "why here?")
            .id
            .clone();
        app.review.save_for(&source).expect("save");

        let response = app.handle_mcp(McpRequestKind::ReplyComment {
            id,
            body: "because".to_owned(),
        });
        assert_eq!(
            response,
            McpResponse::Replied {
                status: "replied".to_owned()
            }
        );
        // the reply persists under the commit source, not the working tree
        let reloaded = diffler_core::store::load_source(&fixture.root, &source).expect("load");
        assert_eq!(reloaded.comments[0].status, CommentStatus::Replied);
        assert_eq!(reloaded.comments[0].replies[0].author, AGENT_AUTHOR);
        assert!(
            app.review
                .session
                .comments
                .iter()
                .all(|c| c.replies.is_empty()),
            "the working-tree comment is untouched"
        );
    }

    #[test]
    fn list_reviews_enumerates_sources_with_counts() {
        let (_fixture, mut app, _id) = app_with_comment();
        let oid = app.status.recent[0].oid.clone();
        let source = ReviewSource::commit(&oid);
        app.review.ensure_source(&source).expect("ensure");
        app.review
            .session_for_mut(&source)
            .add_comment(commit_anchor("src/lib.rs"), "human", "x");
        app.review.save_for(&source).expect("save");
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };

        let McpResponse::Reviews(reviews) = app.handle_mcp(McpRequestKind::ListReviews) else {
            panic!("expected reviews");
        };
        let working = reviews
            .iter()
            .find(|r| r.source == "working")
            .expect("working review");
        assert_eq!(working.open_comments, 1);
        let commit = reviews
            .iter()
            .find(|r| r.source == source.key())
            .expect("commit review");
        assert_eq!(commit.open_comments, 1);
        assert_eq!(commit.label, source.label());
        let walkthrough_source = ReviewSource::walkthrough(&published.id);
        let walkthrough = reviews
            .iter()
            .find(|r| r.source == walkthrough_source.key())
            .expect("walkthrough review, a file in reviews/ like any other");
        assert_eq!(walkthrough.open_comments, 1, "the stop is its own comment");
        assert_eq!(walkthrough.label, "tour", "the walkthrough's own title");
    }

    #[test]
    fn agent_mark_viewed_targets_the_open_review_source() {
        let (_fixture, mut app, _id) = app_with_comment();
        let oid = app.status.recent[0].oid.clone();
        app.open_commit_diff(&oid);

        let response = app.handle_mcp(McpRequestKind::MarkViewed {
            file: "src/lib.rs".to_owned(),
        });
        assert_eq!(response, McpResponse::Ok);

        let source = ReviewSource::commit(&oid);
        assert!(
            app.review
                .session_for(&source)
                .viewed
                .contains_key("src/lib.rs"),
            "viewed lands on the open commit review"
        );
        assert!(
            app.review.session.viewed.is_empty(),
            "the working-tree review is untouched"
        );
    }

    #[test]
    fn feedback_returns_open_and_replied_but_not_resolved() {
        let (_fixture, mut app, id) = app_with_comment();
        app.review.session.add_comment(
            Anchor {
                file: "todo.md".to_owned(),
                line: None,
                line_end: None,
                on_old_side: false,
                line_text: None,
            },
            "human",
            "second",
        );
        app.review.session.resolve(&id);
        let McpResponse::Feedback { comments, .. } = app.handle_mcp(McpRequestKind::Feedback)
        else {
            panic!("expected comments");
        };
        assert_eq!(comments.len(), 1);
        assert_eq!(comments[0].body, "second");
    }

    fn stop(title: &str, anchor: Option<&str>, body: &str) -> StopParams {
        StopParams {
            id: None,
            title: title.to_owned(),
            anchor: anchor.map(str::to_owned),
            body: body.to_owned(),
            notes: None,
        }
    }

    fn note(anchor: Option<&str>, body: &str) -> NoteParams {
        NoteParams {
            id: None,
            anchor: anchor.map(str::to_owned),
            body: body.to_owned(),
        }
    }

    fn stop_with_notes(
        title: &str,
        anchor: Option<&str>,
        body: &str,
        notes: Vec<NoteParams>,
    ) -> StopParams {
        let mut stop = stop(title, anchor, body);
        stop.notes = Some(notes);
        stop
    }

    /// The session of the walkthrough source `id` names, once ensured.
    fn walkthrough_session<'a>(app: &'a mut App, id: &str) -> &'a diffler_core::session::Session {
        let source = ReviewSource::walkthrough(id);
        app.review.ensure_source(&source).expect("ensure source");
        app.review.session_for(&source)
    }

    /// The comment ids the walkthrough `id` is made of, in reading order.
    fn stop_ids(app: &mut App, id: &str) -> Vec<String> {
        walkthrough_session(app, id)
            .walkthrough
            .as_ref()
            .map_or_else(Vec::new, |walkthrough| walkthrough.stops.clone())
    }

    #[test]
    fn publishing_three_stops_stores_them_and_review_status_reports_the_count() {
        let (_fixture, mut app, _id) = app_with_comment();
        let stops = (0..3)
            .map(|i| stop(&format!("stop {i}"), None, "why"))
            .collect();
        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "tour".to_owned(),
            stops,
            skipped: None,
            summary: None,
        });
        let McpResponse::WalkthroughPublished(published) = response else {
            panic!("expected a published walkthrough: {response:?}");
        };
        assert_eq!(published.stops, 3);
        assert!(published.receipts.is_empty(), "{:?}", published.receipts);

        let McpResponse::Status(status) = app.handle_mcp(McpRequestKind::ReviewStatus) else {
            panic!("expected a status response");
        };
        let walkthrough = status.walkthroughs.first().expect("a walkthrough");
        assert_eq!(walkthrough.stops, 3);
        assert_eq!(walkthrough.title, "tour");
    }

    /// A stop is an agent comment, which is what puts it in the comments pane
    /// and gives the human a thread to answer it in.
    #[test]
    fn publishing_makes_one_agent_comment_per_stop() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![
                    stop("The answer", Some("src/lib.rs#answer"), "why 42"),
                    stop("What is left", None, "an overview"),
                ],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        let ids = stop_ids(&mut app, &published.id);
        assert_eq!(ids.len(), 2);
        let session = walkthrough_session(&mut app, &published.id);
        let stops: Vec<_> = ids
            .iter()
            .map(|id| session.comment(id).expect("a stop comment"))
            .collect();
        assert_eq!(stops[0].author, AGENT_AUTHOR);
        assert_eq!(stops[0].title.as_deref(), Some("The answer"));
        assert_eq!(stops[0].anchor_ref.as_deref(), Some("src/lib.rs#answer"));
        assert_eq!(stops[0].anchor.file, "src/lib.rs");
        assert_eq!(stops[0].anchor.line, None, "the worker fills the lines");
        assert_eq!(stops[1].anchor_ref, None);
        assert_eq!(
            stops[1].anchor.file, "src/lib.rs",
            "an anchorless stop hangs on the first file the walkthrough names"
        );
    }

    /// Publishing twice with no `id` creates two separate walkthrough
    /// sources, side by side, rather than replacing the first.
    #[test]
    fn publishing_twice_without_an_id_gives_two_walkthroughs() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(first) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "first tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        let McpResponse::WalkthroughPublished(second) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "second tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        assert_ne!(first.id, second.id);
        assert!(
            walkthrough_session(&mut app, &first.id)
                .walkthrough
                .is_some()
        );
        assert!(
            walkthrough_session(&mut app, &second.id)
                .walkthrough
                .is_some()
        );
    }

    /// An id an agent supplies is arbitrary text, not a filesystem-safe oid: a
    /// `/` in it must not turn the review key into a path whose directory
    /// does not exist. The walkthrough still saves, and a fresh read of the
    /// same id finds it.
    #[test]
    fn a_walkthrough_id_with_a_slash_still_saves_and_loads() {
        let (fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: Some("feature/login".to_owned()),
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        assert_eq!(published.id, "feature/login");

        let source = ReviewSource::walkthrough("feature/login");
        let reloaded = diffler_core::store::load_source(&fixture.root, &source).expect("load");
        assert!(
            reloaded.walkthrough.is_some(),
            "the walkthrough landed on disk"
        );

        let McpResponse::Walkthrough(Some(info)) = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some("feature/login".to_owned()),
        }) else {
            panic!("expected the walkthrough back by its own id");
        };
        assert_eq!(info.title, "tour");
    }

    /// A publish that cannot write its review file must not claim success:
    /// the agent is told the save failed rather than being handed an id for
    /// a walkthrough that was never persisted.
    #[cfg(unix)]
    #[test]
    fn a_walkthrough_that_fails_to_save_is_reported_not_claimed_published() {
        use std::os::unix::fs::PermissionsExt;

        let (fixture, mut app, _id) = app_with_comment();
        let reviews_dir = fixture.root.join(".diffler/reviews");
        std::fs::create_dir_all(&reviews_dir).expect("create reviews dir");
        std::fs::set_permissions(&reviews_dir, std::fs::Permissions::from_mode(0o555))
            .expect("make the reviews dir read-only");

        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: Some("boom".to_owned()),
            title: "tour".to_owned(),
            stops: vec![stop("one", None, "why")],
            skipped: None,
            summary: None,
        });

        std::fs::set_permissions(&reviews_dir, std::fs::Permissions::from_mode(0o755))
            .expect("restore permissions for cleanup");

        assert!(
            matches!(response, McpResponse::Error(_)),
            "a save that fails must not be reported as published: {response:?}"
        );
    }

    /// Revising is how an agent answers feedback: passing the walkthrough's
    /// own `id` back revises that exact source, leaving any other walkthrough
    /// untouched; a stop that passes its own id back keeps its thread, and
    /// the stops it drops go with the old revision.
    #[test]
    fn republishing_with_the_walkthroughs_id_revises_it_and_leaves_the_other_one() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(first) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("first", None, "why"), stop("second", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        let ids = stop_ids(&mut app, &first.id);
        let kept = ids[0].clone();
        let dropped = ids[1].clone();
        {
            let source = ReviewSource::walkthrough(&first.id);
            app.review
                .session_for_mut(&source)
                .reply(&kept, "reviewer", "say more");
        }

        let McpResponse::WalkthroughPublished(other) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "an unrelated tour".to_owned(),
                stops: vec![stop("elsewhere", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };

        let mut revised = stop("first, revised", Some("src/lib.rs#answer"), "a better why");
        revised.id = Some(kept.clone());
        app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: Some(first.id.clone()),
            title: "tour".to_owned(),
            stops: vec![revised, stop("third", None, "why")],
            skipped: None,
            summary: None,
        });

        let session = walkthrough_session(&mut app, &first.id);
        let comment = session.comment(&kept).expect("the kept stop");
        assert_eq!(comment.replies.len(), 1, "the thread survived");
        assert_eq!(comment.title.as_deref(), Some("first, revised"));
        assert_eq!(comment.body, "a better why");
        assert_eq!(comment.anchor_ref.as_deref(), Some("src/lib.rs#answer"));
        assert!(
            session.comment(&dropped).is_none(),
            "a stop nobody passed back is gone"
        );
        assert_eq!(
            session.walkthrough.as_ref().and_then(|w| w.stops.first()),
            Some(&kept)
        );
        assert!(
            walkthrough_session(&mut app, &other.id)
                .walkthrough
                .is_some(),
            "the unrelated walkthrough is untouched, still there"
        );
    }

    #[test]
    fn publishing_one_stop_over_the_rail_is_refused() {
        let (_fixture, mut app, _id) = app_with_comment();
        let stops = (0..=diffler_core::walkthrough::MAX_STOPS)
            .map(|i| stop(&format!("s{i}"), None, "why"))
            .collect();
        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "too many".to_owned(),
            stops,
            skipped: None,
            summary: None,
        });
        let McpResponse::Error(text) = response else {
            panic!("expected a refusal: {response:?}");
        };
        assert!(text.contains("too_many_stops"), "{text}");
        assert!(
            app.review
                .all_reviews()
                .expect("all reviews")
                .into_iter()
                .all(
                    |(s, session)| !matches!(s, ReviewSource::Walkthrough { .. })
                        || session.walkthrough.is_none()
                ),
            "a refused walkthrough is never stored"
        );
    }

    #[test]
    fn publishing_with_a_summary_stores_it_and_get_walkthrough_returns_it() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: Some("the shape of the change".to_owned()),
            })
        else {
            panic!("expected a published walkthrough");
        };

        let McpResponse::Walkthrough(Some(info)) = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some(published.id),
        }) else {
            panic!("expected a walkthrough");
        };
        assert_eq!(info.summary.as_deref(), Some("the shape of the change"));
    }

    #[test]
    fn publishing_with_no_summary_leaves_it_none() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };

        let McpResponse::Walkthrough(Some(info)) = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some(published.id),
        }) else {
            panic!("expected a walkthrough");
        };
        assert_eq!(info.summary, None);
    }

    /// Publishing stamps the walkthrough with the full oid of whatever is
    /// checked out right now, so its anchors resolve against the code they
    /// actually describe even after the branch moves on. Both the publish
    /// response and a later `get_walkthrough` report it.
    #[test]
    fn publishing_stamps_the_current_head() {
        let (_fixture, mut app, _id) = app_with_comment();
        let head = app.review.vcs.resolve("HEAD").expect("resolve head");
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        assert_eq!(published.rev, Some(head.clone()));

        let McpResponse::Walkthrough(Some(info)) = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some(published.id),
        }) else {
            panic!("expected a walkthrough");
        };
        assert_eq!(info.rev, Some(head));
    }

    /// A revision restamps to whatever HEAD is now: it redescribes the stops
    /// against the code currently checked out, not the code an earlier
    /// revision of this same walkthrough was pinned to.
    #[test]
    fn republishing_restamps_to_the_new_head() {
        let (fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(first) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };

        fixture.write("todo.md", "- [ ] more\n");
        fixture.commit_all("move head on");
        let after = app.review.vcs.resolve("HEAD").expect("resolve head");
        assert_ne!(first.rev, Some(after.clone()), "the fixture moved HEAD");

        let McpResponse::WalkthroughPublished(second) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: Some(first.id.clone()),
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "still why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a revised walkthrough");
        };
        assert_eq!(second.rev, Some(after));
    }

    /// A revision that passes the same summary back keeps it, the way a stop
    /// passing its own id back keeps its thread.
    #[test]
    fn a_revision_that_passes_the_same_summary_back_keeps_it() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: Some("the shape of the change".to_owned()),
            })
        else {
            panic!("expected a published walkthrough");
        };

        app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: Some(published.id.clone()),
            title: "tour, revised".to_owned(),
            stops: vec![stop("one", None, "why")],
            skipped: None,
            summary: Some("the shape of the change".to_owned()),
        });

        let McpResponse::Walkthrough(Some(info)) = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some(published.id),
        }) else {
            panic!("expected a walkthrough");
        };
        assert_eq!(info.summary.as_deref(), Some("the shape of the change"));
    }

    #[test]
    fn an_oversized_summary_is_refused_with_the_body_cap_receipt() {
        let (_fixture, mut app, _id) = app_with_comment();
        let oversized = "x".repeat(BODY_MAX_BYTES + 1);
        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "tour".to_owned(),
            stops: vec![stop("one", None, "why")],
            skipped: None,
            summary: Some(oversized),
        });
        let McpResponse::Error(text) = response else {
            panic!("expected a refusal: {response:?}");
        };
        assert!(text.contains("body_too_long"), "{text}");
        assert!(
            app.review
                .all_reviews()
                .expect("all reviews")
                .into_iter()
                .all(
                    |(s, session)| !matches!(s, ReviewSource::Walkthrough { .. })
                        || session.walkthrough.is_none()
                ),
            "a refused walkthrough is never stored"
        );
    }

    #[test]
    fn a_bare_path_anchor_yields_an_anchor_whole_receipt() {
        let (_fixture, mut app, _id) = app_with_comment();
        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "tour".to_owned(),
            stops: vec![stop("the file", Some("src/lib.rs"), "why")],
            skipped: None,
            summary: None,
        });
        let McpResponse::WalkthroughPublished(published) = response else {
            panic!("expected a published walkthrough: {response:?}");
        };
        assert!(
            published
                .receipts
                .iter()
                .any(|r| r.code == "anchor_whole" && r.stop == Some(0)),
            "{:?}",
            published.receipts
        );
    }

    #[test]
    fn a_sequence_diagram_fence_yields_a_figure_dropped_receipt() {
        let (_fixture, mut app, _id) = app_with_comment();
        let body = "```mermaid\nsequenceDiagram\n  a->>b: hi\n```\n";
        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "tour".to_owned(),
            stops: vec![stop("a figure", None, body)],
            skipped: None,
            summary: None,
        });
        let McpResponse::WalkthroughPublished(published) = response else {
            panic!("expected a published walkthrough: {response:?}");
        };
        assert!(
            published
                .receipts
                .iter()
                .any(|r| r.code == "figure_dropped"),
            "{:?}",
            published.receipts
        );
    }

    #[test]
    fn get_walkthrough_round_trips() {
        let (_fixture, mut app, _id) = app_with_comment();
        assert_eq!(
            app.handle_mcp(McpRequestKind::GetWalkthrough { id: None }),
            McpResponse::Walkthrough(None)
        );
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", Some("src/lib.rs#answer"), "why")],
                skipped: Some("left out the rest".to_owned()),
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        let McpResponse::Walkthrough(Some(info)) =
            app.handle_mcp(McpRequestKind::GetWalkthrough { id: None })
        else {
            panic!("expected a walkthrough");
        };
        assert_eq!(info.title, "tour");
        assert_eq!(info.skipped.as_deref(), Some("left out the rest"));
        assert_eq!(info.stops.len(), 1);
        assert_eq!(info.stops[0].title, "one");
        assert_eq!(info.stops[0].anchor.as_deref(), Some("src/lib.rs#answer"));
        assert_eq!(
            Some(&info.stops[0].id),
            stop_ids(&mut app, &published.id).first(),
            "the comment id, which is what a revision passes back"
        );
    }

    /// An id nobody has published under is a real "no such walkthrough",
    /// distinct from a read that failed.
    #[test]
    fn get_walkthrough_by_an_unknown_id_returns_none_not_an_error() {
        let (_fixture, mut app, _id) = app_with_comment();
        let response = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some("nope".to_owned()),
        });
        assert_eq!(response, McpResponse::Walkthrough(None));
    }

    /// A review file that fails to parse is a real error, not the same
    /// "nothing here" a genuinely unpublished id answers with: an id
    /// `review_status` just listed must never come back silently null.
    #[test]
    fn get_walkthrough_surfaces_a_read_failure_instead_of_none() {
        let (fixture, mut app, _id) = app_with_comment();
        std::fs::create_dir_all(fixture.root.join(".diffler/reviews")).expect("mkdir");
        std::fs::write(
            fixture
                .root
                .join(".diffler/reviews/walkthrough-broken.json"),
            "{not json",
        )
        .expect("write corrupt file");

        let response = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some("broken".to_owned()),
        });
        assert!(
            matches!(response, McpResponse::Error(_)),
            "a corrupt file must not read back as no walkthrough: {response:?}"
        );
    }

    /// `get_walkthrough` fetches a specific walkthrough by id, or the newest
    /// one when `id` is omitted.
    #[test]
    fn get_walkthrough_by_id_and_by_default() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(first) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "first tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        let McpResponse::WalkthroughPublished(second) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "second tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        // both publishes can land in the same wall-clock second; force them
        // apart so "the newest one" has something real to pick
        if let Some(w) = app
            .review
            .session_for_mut(&ReviewSource::walkthrough(&first.id))
            .walkthrough
            .as_mut()
        {
            w.at = 1;
        }
        if let Some(w) = app
            .review
            .session_for_mut(&ReviewSource::walkthrough(&second.id))
            .walkthrough
            .as_mut()
        {
            w.at = 2;
        }

        let McpResponse::Walkthrough(Some(info)) = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some(first.id.clone()),
        }) else {
            panic!("expected the first walkthrough");
        };
        assert_eq!(info.title, "first tour");

        let McpResponse::Walkthrough(Some(info)) =
            app.handle_mcp(McpRequestKind::GetWalkthrough { id: None })
        else {
            panic!("expected the newest walkthrough");
        };
        assert_eq!(info.title, "second tour", "the default is the newest one");
        assert_eq!(info.id, second.id);
    }

    /// `review_status` lists every walkthrough of the review, newest first.
    #[test]
    fn review_status_lists_every_walkthrough_newest_first() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(first) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "first tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        let McpResponse::WalkthroughPublished(second) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "second tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        // both publishes can land in the same wall-clock second; force them
        // apart so the newest-first sort has something real to sort on
        if let Some(w) = app
            .review
            .session_for_mut(&ReviewSource::walkthrough(&first.id))
            .walkthrough
            .as_mut()
        {
            w.at = 1;
        }
        if let Some(w) = app
            .review
            .session_for_mut(&ReviewSource::walkthrough(&second.id))
            .walkthrough
            .as_mut()
        {
            w.at = 2;
        }

        let McpResponse::Status(status) = app.handle_mcp(McpRequestKind::ReviewStatus) else {
            panic!("expected a status response");
        };
        let titles: Vec<&str> = status
            .walkthroughs
            .iter()
            .map(|w| w.title.as_str())
            .collect();
        assert_eq!(titles, vec!["second tour", "first tour"]);
    }

    /// A corrupt review file must not hide every other review or walkthrough
    /// behind a false "clean repository": `review_status` names the file
    /// instead, and `list_reviews` keeps listing everything that did parse.
    #[test]
    fn a_corrupt_review_file_is_named_not_hidden_and_never_hides_the_rest() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        let reviews_dir = app.review.repo_root.join(".diffler/reviews");
        std::fs::write(reviews_dir.join("walkthrough-broken.json"), "{not json")
            .expect("write corrupt file");

        let McpResponse::Status(status) = app.handle_mcp(McpRequestKind::ReviewStatus) else {
            panic!("expected a status response");
        };
        assert_eq!(status.corrupt_reviews, vec!["walkthrough-broken.json"]);
        assert!(
            status.walkthroughs.iter().any(|w| w.id == published.id),
            "the good walkthrough is still listed"
        );

        let McpResponse::Reviews(reviews) = app.handle_mcp(McpRequestKind::ListReviews) else {
            panic!("expected reviews");
        };
        assert!(
            reviews.iter().any(|r| r.source == "working"),
            "list_reviews is not disrupted by the corrupt file either"
        );
    }

    /// The status screen reads the session's walkthrough at draw time, so a
    /// publish over MCP has to show up on the very next render, with no
    /// keypress in between.
    #[test]
    fn publishing_over_mcp_shows_on_the_status_screen_without_a_keypress() {
        let (_fixture, mut app, _id) = app_with_comment();
        let (reply, mut rx) = tokio::sync::oneshot::channel();
        let flow = app.handle(crate::event::AppEvent::Mcp(crate::mcp::McpRequest {
            kind: McpRequestKind::PublishWalkthrough {
                id: None,
                title: "the tour".to_owned(),
                stops: vec![stop("one", None, "why")],
                skipped: None,
                summary: None,
            },
            reply,
        }));
        assert_eq!(flow, crate::app::Flow::Continue);
        assert!(matches!(
            rx.try_recv(),
            Ok(McpResponse::WalkthroughPublished(_))
        ));

        let content = crate::test_support::render(&mut app).backend().to_string();
        assert!(content.contains("Walkthrough"), "{content}");
    }

    /// A comment on a stop is a reply on that stop's own comment, so the id in
    /// the feedback is how the agent knows which stop the human is talking
    /// about.
    #[test]
    fn feedback_carries_a_reply_on_the_stop_it_answers() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop("one", None, "why"), stop("two", None, "why")],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };
        let second = stop_ids(&mut app, &published.id)[1].clone();
        let source = ReviewSource::walkthrough(&published.id);
        app.review
            .session_for_mut(&source)
            .reply(&second, "reviewer", "why not 43?");

        let McpResponse::Feedback { comments } = app.handle_mcp(McpRequestKind::Feedback) else {
            panic!("expected feedback");
        };
        let answered = comments
            .iter()
            .find(|comment| comment.id == second)
            .expect("the stop the human replied on");
        assert_eq!(answered.replies.len(), 1);
        assert_eq!(answered.replies[0].body, "why not 43?");
    }

    /// A stop's `notes` are its own remarks on other parts of the same
    /// region: each becomes an agent comment beside the stop's, and the
    /// walkthrough owns all of them so a republish can tell them from a
    /// human's.
    #[test]
    fn publishing_a_stops_notes_makes_extra_agent_comments() {
        let (_fixture, mut app, _id) = app_with_comment();
        let McpResponse::WalkthroughPublished(published) =
            app.handle_mcp(McpRequestKind::PublishWalkthrough {
                id: None,
                title: "tour".to_owned(),
                stops: vec![stop_with_notes(
                    "The answer",
                    Some("src/lib.rs#answer"),
                    "why 42",
                    vec![
                        note(None, "a first remark"),
                        note(Some("src/lib.rs:2"), "a second remark"),
                    ],
                )],
                skipped: None,
                summary: None,
            })
        else {
            panic!("expected a published walkthrough");
        };

        let session = walkthrough_session(&mut app, &published.id);
        let walkthrough = session.walkthrough.clone().expect("a walkthrough");
        assert_eq!(walkthrough.stops.len(), 1);
        assert_eq!(session.comments.len(), 3, "{:?}", session.comments);
        let notes: Vec<_> = session
            .comments
            .iter()
            .filter(|c| !walkthrough.stops.contains(&c.id))
            .collect();
        assert_eq!(notes.len(), 2);
        assert!(notes.iter().all(|note| note.author == AGENT_AUTHOR));
        assert!(notes.iter().all(|note| note.title.is_none()));

        let McpResponse::Walkthrough(Some(info)) = app.handle_mcp(McpRequestKind::GetWalkthrough {
            id: Some(published.id),
        }) else {
            panic!("expected a walkthrough");
        };
        assert_eq!(info.stops[0].notes.len(), 2);
    }

    /// A note anchored outside its stop's own file would show up in another
    /// slide entirely, so it is refused rather than silently misplaced.
    #[test]
    fn a_note_naming_another_file_than_its_stop_is_refused() {
        let (_fixture, mut app, _id) = app_with_comment();
        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "tour".to_owned(),
            stops: vec![stop_with_notes(
                "The answer",
                Some("src/lib.rs#answer"),
                "why 42",
                vec![note(Some("ci.yml:1"), "wrong file")],
            )],
            skipped: None,
            summary: None,
        });
        let McpResponse::Error(text) = response else {
            panic!("expected a refusal: {response:?}");
        };
        assert!(text.contains("note_outside_stop"), "{text}");
        assert!(
            app.review
                .all_reviews()
                .expect("all reviews")
                .into_iter()
                .all(
                    |(s, session)| !matches!(s, ReviewSource::Walkthrough { .. })
                        || session.walkthrough.is_none()
                ),
            "a refused walkthrough is never stored"
        );
    }

    /// Two stops passing back the same id would have the second write find
    /// the comment the first just made and overwrite it, silently losing the
    /// first stop. Refused instead, naming the repeated id.
    #[test]
    fn two_stops_sharing_an_id_are_refused() {
        let (_fixture, mut app, _id) = app_with_comment();
        let mut first = stop("first", None, "why");
        first.id = Some("shared".to_owned());
        let mut second = stop("second", None, "why");
        second.id = Some("shared".to_owned());
        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "tour".to_owned(),
            stops: vec![first, second],
            skipped: None,
            summary: None,
        });
        let McpResponse::Error(text) = response else {
            panic!("expected a refusal: {response:?}");
        };
        assert!(text.contains("duplicate_id"), "{text}");
        assert!(text.contains("shared"), "{text}");
        assert!(
            app.review
                .all_reviews()
                .expect("all reviews")
                .into_iter()
                .all(
                    |(s, session)| !matches!(s, ReviewSource::Walkthrough { .. })
                        || session.walkthrough.is_none()
                ),
            "a refused walkthrough is never stored"
        );
    }

    /// The same collision applies between a stop and one of its own notes:
    /// nothing distinguishes their ids from each other in the write loop.
    #[test]
    fn a_stop_and_its_own_note_sharing_an_id_are_refused() {
        let (_fixture, mut app, _id) = app_with_comment();
        let mut collides = note(None, "a remark");
        collides.id = Some("shared".to_owned());
        let mut stop = stop_with_notes("the stop", None, "why", vec![collides]);
        stop.id = Some("shared".to_owned());
        let response = app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "tour".to_owned(),
            stops: vec![stop],
            skipped: None,
            summary: None,
        });
        let McpResponse::Error(text) = response else {
            panic!("expected a refusal: {response:?}");
        };
        assert!(text.contains("duplicate_id"), "{text}");
    }

    /// A note's thread survives a revision the same way a stop's does: pass
    /// its id back to keep it, leave it out and it goes with the rest of the
    /// old walkthrough.
    #[test]
    fn republishing_keeps_a_note_passed_back_by_id_and_deletes_the_rest() {
        let (_fixture, mut app, _id) = app_with_comment();
        app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: None,
            title: "tour".to_owned(),
            stops: vec![stop_with_notes(
                "The answer",
                Some("src/lib.rs#answer"),
                "why 42",
                vec![note(None, "first"), note(None, "second")],
            )],
            skipped: None,
            summary: None,
        });
        let McpResponse::Walkthrough(Some(info)) =
            app.handle_mcp(McpRequestKind::GetWalkthrough { id: None })
        else {
            panic!("expected a walkthrough");
        };
        let walkthrough_id = info.id.clone();
        let stop_id = info.stops[0].id.clone();
        let kept_note = info.stops[0].notes[0].id.clone();
        let dropped_note = info.stops[0].notes[1].id.clone();
        let source = ReviewSource::walkthrough(&walkthrough_id);
        app.review
            .session_for_mut(&source)
            .reply(&kept_note, "reviewer", "say more");

        let mut kept = note(None, "first, revised");
        kept.id = Some(kept_note.clone());
        let mut revised = stop_with_notes(
            "The answer",
            Some("src/lib.rs#answer"),
            "why 42, revised",
            vec![kept],
        );
        revised.id = Some(stop_id);
        app.handle_mcp(McpRequestKind::PublishWalkthrough {
            id: Some(walkthrough_id.clone()),
            title: "tour".to_owned(),
            stops: vec![revised],
            skipped: None,
            summary: None,
        });

        let session = app.review.session_for(&source);
        let kept = session.comment(&kept_note).expect("the kept note");
        assert_eq!(kept.replies.len(), 1, "the thread survived");
        assert_eq!(kept.body, "first, revised");
        assert!(
            session.comment(&dropped_note).is_none(),
            "a note nobody passed back is gone"
        );
    }
}