flake-edit 0.3.6

Edit your flake inputs with ease.
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
//! Auto-deduplication of nested follows.
//!
//! Three structs thread through the pipeline:
//!
//! - `AnalysisCtx`: read-only inputs (graph, config, bounds).
//! - `FollowPlan`: mutable accumulator threaded through collection.
//! - `AppliedPlan`: outcomes of the apply step, consumed by `render_summary`.

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

use crate::change::{Change, ChangeId};
use crate::config::FollowConfig;
use crate::edit::{FlakeEdit, InputMap};
use crate::follows::{
    AttrPath, Edge, EdgeOrigin, FollowsGraph, Segment, is_follows_reference_to_parent,
};
use crate::input::Range;
use crate::lock::{FlakeLock, NestedInput};
use crate::validate;

use super::super::super::editor::Editor;
use super::super::super::state::AppState;
use super::super::{Error, Result};
use super::load_follow_context;

const SENTINEL_ALREADY_DEDUPLICATED: &str = "All inputs are already deduplicated.";

/// Entry point for `flake-edit follow` on a single in-memory flake.
pub fn run(editor: &Editor, flake_edit: &mut FlakeEdit, state: &AppState) -> Result<()> {
    run_impl(editor, flake_edit, state, false)
}

/// Run auto-follow against in-memory text.
///
/// This is exposed for benchmark and library callers that need the same
/// planner and in-memory edit path as `flake-edit follow` without file I/O.
#[doc(hidden)]
pub fn run_in_memory(
    flake_text: &str,
    lock_text: &str,
    follow_config: &FollowConfig,
) -> Result<Option<String>> {
    let mut flake_edit = FlakeEdit::from_text(flake_text)?;
    let lock = FlakeLock::read_from_str(lock_text)?;
    let nested_inputs = lock.nested_inputs();
    if nested_inputs.is_empty() {
        return Ok(None);
    }

    let inputs = flake_edit.list().clone();
    if inputs.is_empty() {
        return Ok(None);
    }

    let top_level_inputs: HashSet<String> = inputs.keys().cloned().collect();
    // One [`FlakeLock::nested_inputs`] walk (above) feeds the lock graph and
    // every downstream consumer.
    let lock_graph = FollowsGraph::from_nested_inputs(&nested_inputs);
    let graph = FollowsGraph::from_declared_and_lock_graph(&inputs, &lock_graph);
    let Some(plan) = build_plan(
        flake_text,
        &nested_inputs,
        top_level_inputs,
        &inputs,
        &graph,
        follow_config,
    ) else {
        return Ok(None);
    };

    let applied = apply_plan_text(flake_text, &inputs, &nested_inputs, &lock_graph, &plan)?;
    Ok((applied.current_text != flake_text).then_some(applied.current_text))
}

/// Entry point for batch mode (`flake-edit follow [PATHS...]`).
///
/// Each file is processed independently with its own [`Editor`] and
/// [`AppState`]; processing continues past per-file failures. Any
/// failures are bundled into a single [`Error::Batch`].
pub fn run_batch(
    paths: &[std::path::PathBuf],
    transitive: Option<usize>,
    depth: Option<usize>,
    args: &crate::cli::CliArgs,
) -> Result<()> {
    use std::path::PathBuf;

    let mut errors: Vec<(PathBuf, Box<Error>)> = Vec::new();

    for flake_path in paths {
        let lock_path = flake_path
            .parent()
            .map(|p| p.join("flake.lock"))
            .unwrap_or_else(|| PathBuf::from("flake.lock"));

        let editor = match Editor::from_path(flake_path.clone()) {
            Ok(e) => e,
            Err(source) => {
                errors.push((
                    flake_path.clone(),
                    Box::new(Error::FlakeNotFound {
                        path: flake_path.clone(),
                        source,
                    }),
                ));
                continue;
            }
        };

        let mut flake_edit = match editor.create_flake_edit() {
            Ok(fe) => fe,
            Err(e) => {
                errors.push((flake_path.clone(), Box::new(e.into())));
                continue;
            }
        };

        let mut state = match AppState::new(flake_path.clone(), args.config().map(PathBuf::from)) {
            Ok(s) => s
                .with_diff(args.diff())
                .with_no_lock(args.no_lock())
                .with_lock_offline(true)
                .with_interactive(false)
                .with_lock_file(Some(lock_path))
                .with_no_cache(args.no_cache())
                .with_cache_path(args.cache().map(PathBuf::from)),
            Err(e) => {
                errors.push((flake_path.clone(), Box::new(e.into())));
                continue;
            }
        };

        if let Some(min) = transitive {
            state.config.follow.transitive_min = min;
        }
        if let Some(max) = depth {
            state.config.follow.max_depth = Some(max);
        }

        if let Err(e) = run_impl(&editor, &mut flake_edit, &state, true) {
            errors.push((flake_path.clone(), Box::new(e)));
        }
    }

    if errors.is_empty() {
        Ok(())
    } else {
        Err(Error::Batch { failures: errors })
    }
}

/// Inputs threaded through analysis.
///
/// `top_level_inputs` is owned because [`run_impl`] extends it with names
/// minted by [`emit_direct_promotions`] before the later passes consult it
/// (see the pass ordering in [`run_impl`]). The other fields are derived
/// once from the on-disk text and the lockfile and stay put.
struct AnalysisCtx<'a> {
    nested_inputs: &'a [NestedInput],
    /// Top-level input names. Initialized from the parsed `flake.nix` and
    /// extended in-place between [`emit_direct_promotions`] and the
    /// passes that read it ([`collect_transitive_groups`] and
    /// [`collect_direct_candidates`]).
    top_level_inputs: HashSet<String>,
    /// Full input map. Cycle detection needs URLs.
    inputs: &'a crate::edit::InputMap,
    /// Lock-augmented graph: union of edges declared in `flake.nix` and edges
    /// the lockfile resolved. Used for cycle detection and stale-edge queries
    /// that need the fully-resolved view.
    graph: &'a FollowsGraph,
    /// Edges already written in the `flake.nix` source. Distinct from `graph`
    /// because a follows existing only in the lockfile (declared by an
    /// upstream flake) is still a candidate to write into the user's
    /// `flake.nix`.
    existing_follows: &'a HashSet<AttrPath>,
    follow_config: &'a FollowConfig,
    /// Maximum depth of follows declarations to write. `None` (the default)
    /// writes follows at every depth the lockfile graph supports. `Some(n)`
    /// caps emission at depth `n`.
    max_depth: Option<usize>,
    transitive_min: usize,
}

/// Mutable plan accumulated across the collection functions.
///
/// The four output buckets (`to_follow`, `to_unfollow`, `toplevel_follows`,
/// `toplevel_adds`) feed [`apply_plan_text`]. `seen_nested` is a dedup guard
/// threaded through [`collect_direct_candidates`],
/// [`collect_transitive_groups`], [`collect_direct_groups`],
/// [`emit_direct_promotions`], and [`emit_transitive_promotions`] to prevent
/// scheduling the same nested path twice
/// when both a direct-name match and a transitive group claim it.
/// It is dedup state, not applied output, so [`FollowPlan::has_pending`]
/// excludes it.
#[derive(Default)]
struct FollowPlan {
    to_follow: Vec<(AttrPath, AttrPath)>,
    to_unfollow: Vec<AttrPath>,
    toplevel_follows: Vec<(AttrPath, AttrPath)>,
    toplevel_adds: Vec<(String, String)>,
    seen_nested: HashSet<AttrPath>,
}

impl FollowPlan {
    /// True if at least one applicable change was scheduled. `seen_nested`
    /// is dedup state, not pending output, so it is excluded.
    fn has_pending(&self) -> bool {
        !self.to_follow.is_empty()
            || !self.to_unfollow.is_empty()
            || !self.toplevel_follows.is_empty()
            || !self.toplevel_adds.is_empty()
    }
}

/// What [`apply_plan_text`] committed to the working text.
#[derive(Default)]
struct AppliedPlan {
    /// Working text after every successful change. Equal to the original
    /// when no per-step change applied.
    current_text: String,
    /// `(source_path, target)` follows that were successfully applied,
    /// for the success summary.
    applied_follows: Vec<(AttrPath, AttrPath)>,
    /// Stale follows declarations that were removed.
    unfollowed: Vec<AttrPath>,
    /// Validation warnings observed across speculative applications, in
    /// arrival order. The caller deduplicates for display.
    warnings: Vec<validate::ValidationError>,
}

fn run_impl(
    editor: &Editor,
    flake_edit: &mut FlakeEdit,
    state: &AppState,
    quiet: bool,
) -> Result<()> {
    let Some(ctx) = load_follow_context(flake_edit, state)? else {
        if !quiet {
            println!("Nothing to deduplicate.");
        }
        return Ok(());
    };

    let lock_graph = FollowsGraph::from_nested_inputs(&ctx.nested_inputs);

    // Emission must not be suppressed for an edge that the
    // lock resolved but the source never declared.
    let graph = FollowsGraph::from_declared_and_lock_graph(&ctx.inputs, &lock_graph);

    let Some(plan) = build_plan(
        &editor.text(),
        &ctx.nested_inputs,
        ctx.top_level_inputs.clone(),
        &ctx.inputs,
        &graph,
        &state.config.follow,
    ) else {
        if !quiet {
            println!("{SENTINEL_ALREADY_DEDUPLICATED}");
        }
        return Ok(());
    };

    let applied = apply_plan_text(
        &editor.text(),
        &ctx.inputs,
        &ctx.nested_inputs,
        &lock_graph,
        &plan,
    )?;
    render_summary(editor, state, &applied, quiet)
}

fn build_plan(
    source_text: &str,
    nested_inputs: &[NestedInput],
    top_level_inputs: HashSet<String>,
    inputs: &InputMap,
    graph: &FollowsGraph,
    follow_config: &FollowConfig,
) -> Option<FollowPlan> {
    // Filter the merged graph by `EdgeOrigin::Declared` rather than rebuilding
    // a separate declared-only graph. The declared subset is already in
    // `graph`. Use [`FollowsGraph::declared_sources`] (not
    // [`FollowsGraph::declared_edges`]) so nulled `follows = ""`
    // declarations also count as already-handled and the
    // auto-deduplicator does not silently retarget them.
    let existing_follows: HashSet<AttrPath> = graph.declared_sources();

    let transitive_min = follow_config.transitive_min();
    let max_depth = follow_config.max_depth;

    // Seeding runs against the original `graph`: the post-removal clone
    // built below would chicken-and-egg this loop.
    let to_unfollow = seed_unfollow_set(graph, max_depth);

    // Discovery must see the post-removal graph. Without this, an edge
    // marked for removal still shapes the cycle and routing checks
    // below and can suppress a candidate the apply phase would
    // otherwise unblock, forcing a second `flake-edit follow`
    // invocation to converge. The clone is a discovery-only artifact:
    // the apply phase still runs `Change::Remove` against the
    // unmodified `editor.text()`.
    let mut graph_for_discovery = graph.clone();
    graph_for_discovery.drop_edges_with_sources(&to_unfollow);

    let mut ax = AnalysisCtx {
        nested_inputs,
        top_level_inputs,
        inputs,
        graph: &graph_for_discovery,
        existing_follows: &existing_follows,
        follow_config,
        max_depth,
        transitive_min,
    };

    let mut plan = FollowPlan {
        to_unfollow,
        ..FollowPlan::default()
    };

    // Run the minter (`emit_direct_promotions`) first and extend
    // `ax.top_level_inputs` with its new names before the readers
    // (`emit_transitive_promotions`, `collect_direct_candidates`) consult
    // it. Otherwise the readers would need a second pipeline iteration to
    // see the minted names.
    if transitive_min > 0 {
        let direct_groups = collect_direct_groups(&ax, &plan);
        emit_direct_promotions(&ax, source_text, direct_groups, &mut plan);

        for (name, _) in &plan.toplevel_adds {
            ax.top_level_inputs.insert(name.clone());
        }

        let transitive_groups = collect_transitive_groups(&ax, &plan);
        emit_transitive_promotions(&ax, transitive_groups, &mut plan);
    }

    collect_direct_candidates(&ax, &mut plan);

    scrub_redundant(&graph_for_discovery, &mut plan);

    if !plan.has_pending() {
        return None;
    }

    Some(plan)
}

/// Seed the auto-follow plan's `to_unfollow` set with sources that
/// should be removed before discovery runs.
///
/// Two removal classes are collected: stale declared edges (source
/// absent from the lockfile, including target-less `follows = ""`
/// twins that skip [`FollowsGraph::declared_edges`]) and depth-N
/// edges that the lockfile already routes via upstream propagation.
/// The [`FollowsGraph::lock_routes_to`] call passes `Some(edge)` so
/// the edge under test is excluded; without that the user's own
/// declaration counts as the route and every declared edge would
/// qualify.
///
/// Result is lex-sorted and deduplicated.
fn seed_unfollow_set(graph: &FollowsGraph, max_depth: Option<usize>) -> Vec<AttrPath> {
    let mut to_unfollow: Vec<AttrPath> = graph
        .stale_edges()
        .into_iter()
        .map(|e| e.source.clone())
        .collect();
    to_unfollow.extend(graph.stale_nulled_sources().into_iter().cloned());
    let stale_set: HashSet<AttrPath> = to_unfollow.iter().cloned().collect();
    for edge in graph.declared_edges() {
        if stale_set.contains(&edge.source) {
            continue;
        }
        // Depth-1 has no upstream parent to propagate from.
        if edge.source.len() < 3 {
            continue;
        }
        if let Some(max) = max_depth
            && edge.source.len() > max + 1
        {
            continue;
        }
        if graph.lock_routes_to(&edge.source, &edge.follows, Some(edge), &[]) {
            tracing::debug!(
                "Marking redundant follow for removal: {} -> {} (covered by upstream propagation)",
                edge.source,
                edge.follows,
            );
            to_unfollow.push(edge.source.clone());
        }
    }
    to_unfollow.sort();
    to_unfollow.dedup();
    to_unfollow
}

/// Depth-bounded path-shape filter shared by every collection function.
///
/// Path length encodes depth: `parent.nested` is depth 1 (length 2),
/// `parent.middle.grandchild` is depth 2 (length 3). When `max_depth` is
/// `Some(n)`, the bound `len() <= n + 1` admits exactly that depth. When
/// `max_depth` is `None`, every path of length `>= 2` is admitted.
fn within_depth(path: &AttrPath, max_depth: Option<usize>) -> bool {
    if path.len() < 2 {
        return false;
    }
    max_depth.is_none_or(|m| path.len() <= m + 1)
}

/// True when an ancestor of `nested_path` already declares a follows
/// that contradicts the proposed `target`. An ancestor here is a path
/// formed by truncating one or more middle segments while preserving
/// the leading parent and the trailing name.
///
/// "Contradicts" means: the ancestor declares a different follows
/// target, or the ancestor declares `follows = ""` (nulled). In either
/// case the user has explicitly chosen a value for that subtree's
/// mapping of the trailing name, and a deeper proposal pointing
/// somewhere else would silently override that choice.
///
/// Ancestor declarations pointing at the same `target` do not count:
/// they reinforce, rather than override, the deeper proposal, so the
/// auto-deduplicator should still be free to emit the deeper edge.
///
/// For `[parent, mid1, ..., name]` of length `n >= 3`, checks every
/// candidate `[parent, mid1, ..., midk, name]` of length `2..n - 1`.
fn ancestor_overrides_subtree(
    nested_path: &AttrPath,
    target: &AttrPath,
    graph: &FollowsGraph,
) -> bool {
    if nested_path.len() < 3 {
        return false;
    }
    let segs = nested_path.segments();
    let last = nested_path.last();
    let nulled = graph.declared_nulled();
    for prefix_len in 1..nested_path.len() - 1 {
        let mut candidate = AttrPath::new(segs[0].clone());
        for seg in &segs[1..prefix_len] {
            candidate.push(seg.clone());
        }
        candidate.push(last.clone());
        if nulled.contains(&candidate) {
            return true;
        }
        for edge in graph.outgoing(&candidate) {
            if !matches!(edge.origin, EdgeOrigin::Declared { .. }) {
                continue;
            }
            if &edge.follows != target {
                return true;
            }
        }
    }
    false
}

fn collect_direct_candidates(ax: &AnalysisCtx<'_>, plan: &mut FollowPlan) {
    // Shallowest-first: depth-1 emissions must land in `plan.to_follow`
    // before depth-N candidates consult `extra_edges` below.
    let mut iter: Vec<&NestedInput> = ax.nested_inputs.iter().collect();
    iter.sort_by(|a, b| {
        a.path
            .len()
            .cmp(&b.path.len())
            .then_with(|| a.path.cmp(&b.path))
    });
    for nested in iter {
        // Same-run depth-1 emissions can close a depth-N chain.
        let extra_edges: Vec<(AttrPath, AttrPath)> = if nested.path.len() >= 3 {
            plan.to_follow.to_vec()
        } else {
            Vec::new()
        };
        let Some(target_path) = resolve_direct_candidate(ax, nested, &extra_edges) else {
            continue;
        };
        plan.seen_nested.insert(nested.path.clone());
        plan.to_follow.push((nested.path.clone(), target_path));
    }
}

/// `prior_emissions` carries the same-run `to_follow` entries already
/// queued at shallower depths so [`FollowsGraph::lock_routes_to`] sees
/// them when checking depth-N candidates.
fn resolve_direct_candidate(
    ax: &AnalysisCtx<'_>,
    nested: &NestedInput,
    prior_emissions: &[(AttrPath, AttrPath)],
) -> Option<AttrPath> {
    if !within_depth(&nested.path, ax.max_depth) {
        return None;
    }
    let parent = nested.path.first().as_str();
    let nested_name = nested.path.last().as_str();
    let path_display = nested.path.to_string();

    if ax.follow_config.is_ignored(&path_display, nested_name) {
        tracing::debug!("Skipping {}: ignored by config", path_display);
        return None;
    }
    if ax.existing_follows.contains(&nested.path) {
        tracing::debug!("Skipping {}: already follows in flake.nix", path_display);
        return None;
    }

    let target = ax
        .top_level_inputs
        .iter()
        .find(|top| ax.follow_config.can_follow(nested_name, top))?;

    if let Some(target_input) = ax.inputs.get(target.as_str())
        && is_follows_reference_to_parent(target_input.url(), parent)
    {
        tracing::debug!(
            "Skipping {} -> {}: would create cycle (target follows {}/...)",
            path_display,
            target,
            parent,
        );
        return None;
    }

    let target_path = match Segment::from_unquoted(target.clone()) {
        Ok(seg) => AttrPath::new(seg),
        Err(e) => {
            tracing::warn!("Skipping {path_display} -> {target}: invalid input name: {e}");
            return None;
        }
    };

    if ancestor_overrides_subtree(&nested.path, &target_path, ax.graph) {
        tracing::debug!(
            "Skipping {} -> {}: ancestor declares a different follows for the same trailing name",
            path_display,
            target,
        );
        return None;
    }

    // Multi-hop / lockfile-only cycle detection. The URL-prefix check
    // above catches only the immediate-parent case. The merged graph
    // DFS catches the rest.
    let proposed = Edge {
        source: nested.path.clone(),
        follows: target_path.clone(),
        origin: EdgeOrigin::Declared {
            range: Range { start: 0, end: 0 },
        },
    };
    if ax.graph.would_create_cycle(&proposed) {
        tracing::debug!(
            "Skipping {} -> {}: would create cycle (multi-hop or lockfile-resolved)",
            path_display,
            target,
        );
        return None;
    }
    if ax
        .graph
        .lock_routes_to(&nested.path, &target_path, None, prior_emissions)
    {
        tracing::debug!(
            "Skipping {} -> {}: lockfile already routes via upstream propagation",
            path_display,
            target,
        );
        return None;
    }

    Some(target_path)
}

fn collect_transitive_groups(
    ax: &AnalysisCtx<'_>,
    plan: &FollowPlan,
) -> HashMap<String, HashMap<AttrPath, Vec<AttrPath>>> {
    let mut groups: HashMap<String, HashMap<AttrPath, Vec<AttrPath>>> = HashMap::new();

    for nested in ax.nested_inputs.iter() {
        let Some((top_level_name, transitive_target)) =
            resolve_transitive_candidate(ax, plan, nested)
        else {
            continue;
        };
        groups
            .entry(top_level_name)
            .or_default()
            .entry(transitive_target)
            .or_default()
            .push(nested.path.clone());
    }

    groups
}

fn resolve_transitive_candidate(
    ax: &AnalysisCtx<'_>,
    plan: &FollowPlan,
    nested: &NestedInput,
) -> Option<(String, AttrPath)> {
    if !within_depth(&nested.path, ax.max_depth) {
        return None;
    }
    let nested_name = nested.path.last().as_str();
    let parent = nested.path.first().as_str();
    let path_display = nested.path.to_string();

    if ax.follow_config.is_ignored(&path_display, nested_name) {
        return None;
    }
    if ax.existing_follows.contains(&nested.path) || plan.seen_nested.contains(&nested.path) {
        return None;
    }
    // Handled by [`collect_direct_candidates`].
    if ax
        .top_level_inputs
        .iter()
        .any(|top| ax.follow_config.can_follow(nested_name, top))
    {
        return None;
    }

    let transitive_target = nested.follows.as_ref()?;
    if ancestor_overrides_subtree(&nested.path, transitive_target, ax.graph) {
        return None;
    }
    if transitive_target.len() < 2 {
        return None;
    }
    if transitive_target.last().as_str() == nested_name {
        return None;
    }

    let top_level_name = ax
        .follow_config
        .resolve_alias(nested_name)
        .unwrap_or(nested_name)
        .to_string();
    if ax.top_level_inputs.contains(&top_level_name) {
        return None;
    }

    if let Some(target_input) = ax.inputs.get(transitive_target.first().as_str())
        && is_follows_reference_to_parent(target_input.url(), parent)
    {
        return None;
    }

    let proposed = Edge {
        source: nested.path.clone(),
        follows: transitive_target.clone(),
        origin: EdgeOrigin::Declared {
            range: Range { start: 0, end: 0 },
        },
    };
    if ax.graph.would_create_cycle(&proposed) {
        return None;
    }

    Some((top_level_name, transitive_target.clone()))
}

/// When several parents share the same dependency (e.g. `treefmt.nixpkgs`
/// and `treefmt-nix.nixpkgs`), one can be promoted to top-level and the
/// others follow it.
fn collect_direct_groups(
    ax: &AnalysisCtx<'_>,
    plan: &FollowPlan,
) -> HashMap<String, Vec<(AttrPath, Option<String>)>> {
    let mut groups: HashMap<String, Vec<(AttrPath, Option<String>)>> = HashMap::new();

    for nested in ax.nested_inputs.iter() {
        if !within_depth(&nested.path, ax.max_depth) {
            continue;
        }
        if nested.follows.is_some() {
            continue;
        }
        let nested_name = nested.path.last().as_str();
        let path_display = nested.path.to_string();

        if ax.follow_config.is_ignored(&path_display, nested_name) {
            continue;
        }
        if ax.existing_follows.contains(&nested.path) || plan.seen_nested.contains(&nested.path) {
            continue;
        }
        if ax
            .top_level_inputs
            .iter()
            .any(|top| ax.follow_config.can_follow(nested_name, top))
        {
            continue;
        }

        let canonical_name = ax
            .follow_config
            .resolve_alias(nested_name)
            .unwrap_or(nested_name)
            .to_string();
        if ax.top_level_inputs.contains(&canonical_name) {
            continue;
        }

        groups
            .entry(canonical_name)
            .or_default()
            .push((nested.path.clone(), nested.url.clone()));
    }

    groups
}

/// Turn transitive groups into top-level follows and back-fill
/// `plan.to_follow` with the per-nested follows hanging off them.
///
/// Must run after [`emit_direct_promotions`] so the top-level names it
/// minted are visible in `ax.top_level_inputs`.
fn emit_transitive_promotions(
    ax: &AnalysisCtx<'_>,
    transitive_groups: HashMap<String, HashMap<AttrPath, Vec<AttrPath>>>,
    plan: &mut FollowPlan,
) {
    for (top_name, targets) in transitive_groups {
        let mut eligible: Vec<(AttrPath, Vec<AttrPath>)> = targets
            .into_iter()
            .filter(|(_, paths)| paths.len() >= ax.transitive_min)
            .collect();

        if eligible.len() != 1 {
            continue;
        }

        let (target_path, paths) = eligible.pop().unwrap();

        if target_path.to_flake_follows_string() == top_name {
            continue;
        }

        let top_seg = match Segment::from_unquoted(top_name.clone()) {
            Ok(s) => s,
            Err(e) => {
                tracing::warn!(
                    "Skipping toplevel follow promotion for `{top_name}`: invalid segment: {e}"
                );
                continue;
            }
        };
        plan.toplevel_follows
            .push((AttrPath::new(top_seg.clone()), target_path));

        let top_path = AttrPath::new(top_seg);
        for path in paths {
            if plan.seen_nested.insert(path.clone()) {
                plan.to_follow.push((path, top_path.clone()));
            }
        }
    }
}

/// Turn direct-reference groups into new top-level adds and back-fill
/// `plan.to_follow` with the per-nested follows hanging off them. Promote
/// only if at least one follows can be applied (probed via a speculative
/// `apply_change` against `editor.text()`).
///
/// Runs before [`emit_transitive_promotions`] and
/// [`collect_direct_candidates`] so the names this function pushes into
/// `plan.toplevel_adds` can be folded into `ax.top_level_inputs` before
/// the later passes consult it.
fn emit_direct_promotions(
    ax: &AnalysisCtx<'_>,
    source_text: &str,
    direct_groups: HashMap<String, Vec<(AttrPath, Option<String>)>>,
    plan: &mut FollowPlan,
) {
    // Probes only differ in which `Change` they apply, not in the input
    // text, so parse `source_text` once and clone the syntax per probe.
    let probe_parsed = validate::ParsedSource::new(source_text);
    if !probe_parsed.parse_errors.is_empty() {
        return;
    }
    let probe_syntax = probe_parsed.syntax;

    let mut direct_groups_sorted: Vec<_> = direct_groups.into_iter().collect();
    direct_groups_sorted.sort_by(|a, b| a.0.cmp(&b.0));
    for (canonical_name, mut entries) in direct_groups_sorted {
        entries.sort_by(|a, b| a.0.cmp(&b.0));
        let Some((url, target_attr)) =
            decide_direct_promotion(ax, &probe_syntax, &canonical_name, &entries)
        else {
            continue;
        };
        plan.toplevel_adds.push((canonical_name.clone(), url));
        record_direct_promotion_entries(plan, &target_attr, &canonical_name, &entries);
    }
}

/// `probe_syntax` is the original `flake.nix` parsed by the caller
/// once and cloned per iteration. The speculative `apply_change`
/// here only checks that an edit would land; the committing pass
/// runs later against the live working text.
fn decide_direct_promotion(
    ax: &AnalysisCtx<'_>,
    probe_syntax: &rnix::SyntaxNode,
    canonical_name: &str,
    entries: &[(AttrPath, Option<String>)],
) -> Option<(String, AttrPath)> {
    if entries.len() < ax.transitive_min {
        return None;
    }
    let url = entries.iter().find_map(|(_, u)| u.clone())?;
    let target_attr = match Segment::from_unquoted(canonical_name.to_string()) {
        Ok(seg) => AttrPath::new(seg),
        Err(e) => {
            tracing::warn!(
                "Skipping direct-reference promotion for `{canonical_name}`: invalid input name: {e}"
            );
            return None;
        }
    };
    let can_follow = entries.iter().any(|(path, _)| {
        let change = Change::Follows {
            input: ChangeId::new(path.clone()),
            target: target_attr.clone(),
        };
        let mut fe = FlakeEdit::from_syntax(probe_syntax.clone());
        fe.apply_change(change)
            .ok()
            .and_then(|outcome| outcome.text)
            .is_some()
    });
    if !can_follow {
        return None;
    }
    Some((url, target_attr))
}

/// A descendant whose ancestor is also in the group is rewritten by
/// the ancestor's follow, so the descendant is dropped from
/// `plan.to_follow` but still claims `plan.seen_nested` so later
/// passes do not reclaim it.
fn record_direct_promotion_entries(
    plan: &mut FollowPlan,
    target_attr: &AttrPath,
    canonical_name: &str,
    entries: &[(AttrPath, Option<String>)],
) {
    let entry_paths: HashSet<AttrPath> = entries.iter().map(|(p, _)| p.clone()).collect();
    for (path, _) in entries {
        let mut covered_by_ancestor = false;
        let mut anc = path.parent();
        while let Some(a) = anc.clone() {
            if entry_paths.contains(&a) {
                covered_by_ancestor = true;
                break;
            }
            anc = a.parent();
        }
        if covered_by_ancestor {
            tracing::debug!(
                "Skipping promotion {} -> {}: ancestor in same group covers it",
                path,
                canonical_name,
            );
            plan.seen_nested.insert(path.clone());
            continue;
        }
        if plan.seen_nested.insert(path.clone()) {
            plan.to_follow.push((path.clone(), target_attr.clone()));
        }
    }
}

/// Drop entries from [`FollowPlan::to_follow`] whose chain is already
/// covered by the rest of the plan plus the lockfile (passed to
/// [`FollowsGraph::lock_routes_to`] as `extra_edges`).
///
/// Catches cross-entry overlaps that [`collect_direct_candidates`],
/// [`emit_direct_promotions`], and [`emit_transitive_promotions`] cannot see
/// from a single candidate's perspective.
/// Idempotent: dropping entries only shrinks `extra_edges`, so a surviving
/// entry stays surviving.
fn scrub_redundant(graph: &FollowsGraph, plan: &mut FollowPlan) {
    if plan.to_follow.is_empty() {
        return;
    }
    let mut keep: Vec<bool> = vec![true; plan.to_follow.len()];
    let mut changed = true;
    while changed {
        changed = false;
        for i in 0..plan.to_follow.len() {
            if !keep[i] {
                continue;
            }
            let (src_i, target_path) = (plan.to_follow[i].0.clone(), plan.to_follow[i].1.clone());
            let extras: Vec<(AttrPath, AttrPath)> = (0..plan.to_follow.len())
                .filter(|&j| j != i && keep[j])
                .map(|j| (plan.to_follow[j].0.clone(), plan.to_follow[j].1.clone()))
                .collect();
            if graph.lock_routes_to(&src_i, &target_path, None, &extras) {
                tracing::debug!(
                    "Scrubbing {} -> {}: redundant given the rest of the plan",
                    src_i,
                    target_path,
                );
                keep[i] = false;
                changed = true;
            }
        }
    }
    let mut keep_iter = keep.into_iter();
    plan.to_follow.retain(|_| keep_iter.next().unwrap_or(true));
}

/// Pairing `current_text` with its `ParsedSource` lets each iteration
/// share a single rnix parse: each accepted change replaces both fields
/// in lockstep, sparing the next phase a re-parse of identical text.
struct PlanState {
    current_text: String,
    current_parsed: validate::ParsedSource,
    warnings: Vec<validate::ValidationError>,
}

enum StepOutcome {
    /// Validation passed and `PlanState` has been updated. `text_changed`
    /// is false when the change collapsed to the existing text; phases
    /// that record applied work key off this flag to avoid logging a
    /// follows that was already in place.
    Accepted {
        text_changed: bool,
    },
    Rejected(Vec<validate::ValidationError>),
    /// `apply_change` returned `Ok` but produced no text. Distinct from
    /// `ApplyError` because no error was raised: the underlying edit
    /// declined to mutate (e.g. removing a path that does not exist).
    NoText,
    ApplyError(crate::error::Error),
}

impl PlanState {
    /// The temporary [`FlakeEdit`] is built from the current parsed
    /// syntax internally so each phase loop can hand off a `Change` and
    /// inspect the outcome without juggling clone-vs-move of the syntax
    /// tree itself.
    fn try_apply_one(
        &mut self,
        change: Change,
        lock_graph_ref: Option<&FollowsGraph>,
    ) -> StepOutcome {
        let mut temp = FlakeEdit::from_syntax(self.current_parsed.syntax.clone());
        let outcome = match temp.apply_change(change) {
            Ok(o) => o,
            Err(e) => return StepOutcome::ApplyError(e),
        };
        let resulting_text = match outcome.text {
            Some(t) => t,
            None => return StepOutcome::NoText,
        };
        let text_changed = resulting_text != self.current_text;
        let resulting_parsed = validate::ParsedSource::new(&resulting_text);
        let validation = validate::validate_speculative_parsed(
            &resulting_parsed,
            temp.curr_list(),
            lock_graph_ref,
        );
        if validation.is_ok() {
            self.warnings.extend(validation.warnings);
            self.current_text = resulting_text;
            self.current_parsed = resulting_parsed;
            StepOutcome::Accepted { text_changed }
        } else {
            StepOutcome::Rejected(validation.errors)
        }
    }
}

/// Apply each scheduled change against a working text buffer, validating
/// between steps.
///
/// Lock-drift lints run once on the pre-batch text via
/// [`validate::validate_full`]. Per-step validation uses
/// [`validate::validate_speculative`], which skips them: mid-batch the
/// in-memory text contains follows the on-disk lockfile has not seen yet,
/// and re-running the lock-drift lints would flag every in-progress edit
/// as drift.
///
/// The cycle lint runs every step against the post-change `temp.curr_list()`
/// and catches any cycle introduced by the in-progress batch, skipping the
/// offending change.
fn apply_plan_text(
    original_text: &str,
    inputs: &crate::edit::InputMap,
    nested_inputs: &[NestedInput],
    lock_graph: &FollowsGraph,
    plan: &FollowPlan,
) -> Result<AppliedPlan> {
    // Each accepted change replaces `current_parsed` with the post-edit
    // [`validate::ParsedSource`], so the next iteration's walker and
    // validation share a single rnix parse of the new text. The pre-batch
    // validation below reuses this same parse.
    let current_parsed = validate::ParsedSource::new(original_text);
    if !current_parsed.parse_errors.is_empty() {
        return Err(Error::Flake(crate::error::Error::Validation(
            current_parsed.parse_errors.clone(),
        )));
    }

    let mut warnings: Vec<validate::ValidationError> = Vec::new();

    // Lock-drift lints fire only on the pre-batch text. Mid-batch they would
    // flag every in-progress edit as drift against the on-disk lockfile. The
    // prebuilt `lock_graph` and `nested_inputs` avoid re-walking `flake.lock`.
    let pre_validation = validate::validate_full_with_lock_graph(
        &current_parsed,
        inputs,
        Some(lock_graph),
        nested_inputs,
    );
    warnings.extend(pre_validation.warnings);

    // Each [`validate::validate_speculative_parsed`] call clone-and-merges the
    // shared `lock_graph`.
    let lock_graph_ref = Some(lock_graph);

    let mut state = PlanState {
        current_text: original_text.to_owned(),
        current_parsed,
        warnings,
    };

    // Top-level adds must precede follows that name them.
    apply_toplevel_adds(plan, &mut state, lock_graph_ref);
    let applied_follows = apply_follow_changes(plan, &mut state, lock_graph_ref);
    let unfollowed = apply_unfollow_changes(plan, &mut state, lock_graph_ref);

    Ok(AppliedPlan {
        current_text: state.current_text,
        applied_follows,
        unfollowed,
        warnings: state.warnings,
    })
}

fn apply_toplevel_adds(
    plan: &FollowPlan,
    state: &mut PlanState,
    lock_graph_ref: Option<&FollowsGraph>,
) {
    for (id, url) in &plan.toplevel_adds {
        let change_id = match ChangeId::parse(id) {
            Ok(change_id) => change_id,
            Err(e) => {
                tracing::error!("could not add top-level input {id}: invalid id: {e}");
                continue;
            }
        };
        let change = Change::Add {
            id: Some(change_id),
            uri: Some(url.clone()),
            flake: true,
        };
        match state.try_apply_one(change, lock_graph_ref) {
            StepOutcome::Accepted { .. } => {}
            StepOutcome::Rejected(errors) => {
                for err in errors {
                    tracing::error!("could not add top-level input {id}: {err}");
                }
            }
            StepOutcome::NoText => {
                tracing::error!("could not add top-level input {id}");
            }
            StepOutcome::ApplyError(e) => {
                tracing::error!("could not add top-level input {id}: {e}");
            }
        }
    }
}

fn apply_follow_changes(
    plan: &FollowPlan,
    state: &mut PlanState,
    lock_graph_ref: Option<&FollowsGraph>,
) -> Vec<(AttrPath, AttrPath)> {
    let mut follow_changes: Vec<(AttrPath, AttrPath)> = plan.toplevel_follows.clone();
    follow_changes.extend(plan.to_follow.iter().cloned());

    let mut applied_follows: Vec<(AttrPath, AttrPath)> = Vec::new();
    for (input_path, target) in &follow_changes {
        let change = Change::Follows {
            input: ChangeId::new(input_path.clone()),
            target: target.clone(),
        };
        match state.try_apply_one(change, lock_graph_ref) {
            StepOutcome::Accepted { text_changed: true } => {
                applied_follows.push((input_path.clone(), target.clone()));
            }
            // A Change::Follows that produced identical text means the
            // declaration was already in place; recording it as a fresh
            // application would inflate the success summary with a no-op.
            StepOutcome::Accepted {
                text_changed: false,
            } => {}
            StepOutcome::Rejected(errors) => {
                for err in errors {
                    tracing::error!("{}", format_apply_error(input_path, &err));
                }
            }
            StepOutcome::NoText => {
                tracing::error!("could not create follows for {input_path}");
            }
            StepOutcome::ApplyError(e) => {
                tracing::error!("could not apply follows for {input_path}: {e}");
            }
        }
    }
    applied_follows
}

fn apply_unfollow_changes(
    plan: &FollowPlan,
    state: &mut PlanState,
    lock_graph_ref: Option<&FollowsGraph>,
) -> Vec<AttrPath> {
    let mut unfollowed: Vec<AttrPath> = Vec::new();
    for nested_path in &plan.to_unfollow {
        let change = Change::Remove {
            ids: vec![ChangeId::new(nested_path.clone())],
        };
        match state.try_apply_one(change, lock_graph_ref) {
            StepOutcome::Accepted { .. } => unfollowed.push(nested_path.clone()),
            // Validation failures and missing text are silently dropped:
            // unfollow is a best-effort cleanup pass and a stale source
            // that fails to remove is no worse than leaving it in place.
            StepOutcome::Rejected(_) | StepOutcome::NoText => {}
            StepOutcome::ApplyError(e) => {
                tracing::error!("could not remove stale follows for {nested_path}: {e}");
            }
        }
    }
    unfollowed
}

fn render_summary(
    editor: &Editor,
    state: &AppState,
    applied: &AppliedPlan,
    quiet: bool,
) -> Result<()> {
    if !applied.warnings.is_empty() && !quiet {
        let mut seen: HashSet<String> = HashSet::new();
        for warning in &applied.warnings {
            if seen.insert(warning_dedup_key(warning)) {
                eprintln!("warning: {}", warning);
            }
        }
    }

    // Empty plans short-circuit earlier (see [`build_plan`]).
    if applied.current_text == editor.text() {
        if !quiet {
            println!("{SENTINEL_ALREADY_DEDUPLICATED}");
        }
        return Ok(());
    }

    if state.diff {
        let original = editor.text();
        let diff = crate::diff::Diff::new(&original, &applied.current_text);
        diff.compare();
        return Ok(());
    }

    editor.apply_or_diff(&applied.current_text, state)?;

    if quiet {
        return Ok(());
    }

    if !applied.applied_follows.is_empty() {
        println!(
            "Deduplicated {} {}.",
            applied.applied_follows.len(),
            if applied.applied_follows.len() == 1 {
                "input"
            } else {
                "inputs"
            }
        );
        for (input_path, target) in &applied.applied_follows {
            println!("  {} -> {}", input_path, target);
        }
    }

    if !applied.unfollowed.is_empty() {
        println!(
            "Removed {} stale follows {}.",
            applied.unfollowed.len(),
            if applied.unfollowed.len() == 1 {
                "declaration"
            } else {
                "declarations"
            }
        );
        for path in &applied.unfollowed {
            println!("  {} (input no longer exists)", path);
        }
    }

    Ok(())
}

/// Source path of the malformed declaration named by `err`, or `None`
/// for variants that carry no source (parse errors, duplicate attributes).
fn offending_source(err: &validate::ValidationError) -> Option<&AttrPath> {
    use validate::ValidationError as V;
    match err {
        V::FollowsTargetNotToplevel { edge, .. }
        | V::FollowsStale { edge, .. }
        | V::FollowsDepthExceeded { edge, .. } => Some(&edge.source),
        V::FollowsContradiction { edges, .. } => edges.first().map(|e| &e.source),
        V::FollowsCycle { cycle, .. } => cycle.edges.first().map(|e| &e.source),
        V::FollowsStaleLock { source_path, .. } => Some(source_path),
        V::ParseError { .. } | V::DuplicateAttribute(_) => None,
    }
}

/// Format a per-step speculative validation error for stderr.
///
/// `validate_speculative` lints the whole graph, so a pre-existing
/// malformed edge surfaces in every iteration of the apply loop. When the
/// error names a source path other than `applying`, blame the malformed
/// edge's owner so the message points at the actual offender.
fn format_apply_error(applying: &AttrPath, err: &validate::ValidationError) -> String {
    match offending_source(err) {
        Some(source) if source != applying => {
            format!(
                "Malformed follows declaration in {}: {}",
                source.first(),
                err
            )
        }
        _ => format!("Error applying follows for {}: {}", applying, err),
    }
}

/// Stable identity for a follows-related warning, ignoring source-text
/// location. Two warnings with the same lint kind and key fields collapse
/// to one in the auto-follow output even if their reported lines differ
/// across iterations of [`validate::validate_full`].
fn warning_dedup_key(err: &validate::ValidationError) -> String {
    use validate::ValidationError as V;
    match err {
        V::FollowsStale { edge, .. } => format!(
            "stale|{}|{}",
            edge.source,
            edge.follows.to_flake_follows_string()
        ),
        V::FollowsStaleLock {
            source_path,
            declared_target,
            lock_target,
            ..
        } => {
            let lock = lock_target
                .as_ref()
                .map(|t| t.to_flake_follows_string())
                .unwrap_or_default();
            format!(
                "stale-lock|{source_path}|{}|{lock}",
                declared_target.to_flake_follows_string()
            )
        }
        other => format!("other|{other}"),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::FollowConfig;
    use crate::input::{Follows, Input, Range};
    use crate::validate::Location;
    use clap::Parser;

    fn declared_edge(source: &str, follows: &str) -> Edge {
        Edge {
            source: AttrPath::parse(source).expect("source"),
            follows: AttrPath::parse(follows).expect("follows"),
            origin: EdgeOrigin::Declared {
                range: Range { start: 0, end: 0 },
            },
        }
    }

    fn loc() -> Location {
        Location {
            line: 60,
            column: 13,
        }
    }

    fn seg(s: &str) -> Segment {
        Segment::from_unquoted(s).unwrap()
    }

    fn ap(s: &str) -> AttrPath {
        AttrPath::parse(s).unwrap()
    }

    fn input_with_follows(id: &str, follows: Vec<(AttrPath, Option<AttrPath>)>) -> Input {
        let mut input = Input::new(seg(id));
        for (path, target) in follows {
            input.follows.push(Follows::Indirect { path, target });
        }
        input.range = Range { start: 1, end: 2 };
        input
    }

    fn make_input_map(items: Vec<Input>) -> InputMap {
        let mut map = InputMap::new();
        for item in items {
            map.insert(item.id().as_str().to_string(), item);
        }
        map
    }

    /// Lockfile that places `parent.middle` and `parent.middle.nixpkgs`
    /// in `resolved_universe` so neither is flagged stale by
    /// [`seed_unfollow_set`].
    fn parent_middle_lock() -> FlakeLock {
        let lock_text = r#"{
  "nodes": {
    "top": {
      "locked": { "lastModified": 1, "narHash": "", "owner": "o", "repo": "r", "rev": "a", "type": "github" },
      "original": { "owner": "o", "repo": "r", "type": "github" }
    },
    "nixpkgs_2": {
      "locked": { "lastModified": 1, "narHash": "", "owner": "o", "repo": "r", "rev": "b", "type": "github" },
      "original": { "owner": "o", "repo": "r", "type": "github" }
    },
    "middle": {
      "inputs": { "nixpkgs": "nixpkgs_2" },
      "locked": { "lastModified": 1, "narHash": "", "owner": "o", "repo": "r", "rev": "c", "type": "github" },
      "original": { "owner": "o", "repo": "r", "type": "github" }
    },
    "parent": {
      "inputs": { "middle": "middle" },
      "locked": { "lastModified": 1, "narHash": "", "owner": "o", "repo": "r", "rev": "d", "type": "github" },
      "original": { "owner": "o", "repo": "r", "type": "github" }
    },
    "root": {
      "inputs": { "top": "top", "parent": "parent" }
    }
  },
  "root": "root",
  "version": 7
}"#;
        FlakeLock::read_from_str(lock_text).unwrap()
    }

    #[test]
    fn seed_unfollow_set_empty_graph_returns_empty() {
        let graph = FollowsGraph::default();
        let result = seed_unfollow_set(&graph, Some(2));
        assert_eq!(result, Vec::<AttrPath>::new());
    }

    #[test]
    fn seed_unfollow_set_collects_stale_declared_edge() {
        // `home-manager.nixpkgs` is declared with a target but no
        // lockfile is supplied, so resolved_universe is empty and
        // [`FollowsGraph::stale_edges`] flags the source.
        let inputs = make_input_map(vec![input_with_follows(
            "home-manager",
            vec![(ap("nixpkgs"), Some(ap("nixpkgs")))],
        )]);
        let graph = FollowsGraph::from_declared(&inputs);
        let result = seed_unfollow_set(&graph, Some(2));
        assert_eq!(result, vec![ap("home-manager.nixpkgs")]);
    }

    #[test]
    fn seed_unfollow_set_collects_stale_nulled_source() {
        // `follows = ""` records the source on
        // [`FollowsGraph::declared_nulled`], not on
        // [`FollowsGraph::declared_edges`]. Without a lockfile entry the
        // helper must still surface it via
        // [`FollowsGraph::stale_nulled_sources`].
        let inputs = make_input_map(vec![input_with_follows(
            "home-manager",
            vec![(ap("nixpkgs"), None)],
        )]);
        let graph = FollowsGraph::from_declared(&inputs);
        let result = seed_unfollow_set(&graph, Some(2));
        assert_eq!(result, vec![ap("home-manager.nixpkgs")]);
    }

    #[test]
    fn seed_unfollow_set_marks_redundant_depth_n_edge() {
        // Ancestor `parent.middle -> top` rewrites
        // `parent.middle.nixpkgs` to `top.nixpkgs`, so the depth-2
        // declaration `parent.middle.nixpkgs -> top.nixpkgs` is
        // already covered by upstream propagation.
        let inputs = make_input_map(vec![input_with_follows(
            "parent",
            vec![
                (ap("middle"), Some(ap("top"))),
                (ap("middle.nixpkgs"), Some(ap("top.nixpkgs"))),
            ],
        )]);
        let graph = FollowsGraph::from_flake(&inputs, &parent_middle_lock());
        let result = seed_unfollow_set(&graph, Some(2));
        assert_eq!(result, vec![ap("parent.middle.nixpkgs")]);
    }

    #[test]
    fn seed_unfollow_set_mixed_sources_are_sorted_and_deduped() {
        let inputs = make_input_map(vec![
            input_with_follows("home-manager", vec![(ap("nixpkgs"), None)]),
            input_with_follows("nixos-cosmic", vec![(ap("nixpkgs"), Some(ap("nixpkgs")))]),
            input_with_follows(
                "parent",
                vec![
                    (ap("middle"), Some(ap("top"))),
                    (ap("middle.nixpkgs"), Some(ap("top.nixpkgs"))),
                ],
            ),
        ]);
        let graph = FollowsGraph::from_flake(&inputs, &parent_middle_lock());
        let result = seed_unfollow_set(&graph, Some(2));
        assert_eq!(
            result,
            vec![
                ap("home-manager.nixpkgs"),
                ap("nixos-cosmic.nixpkgs"),
                ap("parent.middle.nixpkgs"),
            ],
        );
    }

    #[test]
    fn malformed_edge_blames_its_owner_not_iteration_target() {
        let applying = AttrPath::parse("browservice.flake-utils").unwrap();
        let err = validate::ValidationError::FollowsTargetNotToplevel {
            edge: declared_edge(
                "mac-app-util.cl-nix-lite",
                r#""github:verymucho/cl-nix-lite""#,
            ),
            location: loc(),
        };

        let message = format_apply_error(&applying, &err);

        assert!(
            !message.contains("browservice"),
            "must not blame iteration target, got: {message}",
        );
        assert!(
            message.contains("mac-app-util"),
            "must name the malformed edge's owner, got: {message}",
        );
    }

    #[test]
    fn self_caused_error_keeps_apply_framing() {
        let applying = AttrPath::parse("crane.nixpkgs").unwrap();
        let err = validate::ValidationError::FollowsTargetNotToplevel {
            edge: declared_edge("crane.nixpkgs", "missing"),
            location: loc(),
        };

        let message = format_apply_error(&applying, &err);

        assert!(
            message.contains("Error applying follows for crane.nixpkgs"),
            "self-caused error must keep apply framing, got: {message}",
        );
    }

    #[test]
    fn run_batch_surfaces_every_failure() {
        let tmp = tempfile::tempdir().expect("tempdir");
        let missing_a = tmp.path().join("a/flake.nix");
        let missing_b = tmp.path().join("b/flake.nix");
        let paths = vec![missing_a.clone(), missing_b.clone()];
        let args = crate::cli::CliArgs::parse_from(["flake-edit", "follow"]);

        let err = run_batch(&paths, None, None, &args).expect_err("expected batch failure");
        let Error::Batch { failures } = err else {
            panic!("expected Error::Batch, got: {err:?}");
        };

        assert_eq!(
            failures.len(),
            2,
            "every per-file failure must reach the caller, got: {failures:?}",
        );
        for (path, err) in &failures {
            assert!(
                matches!(err.as_ref(), Error::FlakeNotFound { .. }),
                "expected FlakeNotFound for missing flake.nix at {}, got {err:?}",
                path.display(),
            );
        }
        let collected: Vec<&std::path::PathBuf> = failures.iter().map(|(p, _)| p).collect();
        assert!(collected.contains(&&missing_a));
        assert!(collected.contains(&&missing_b));
    }

    /// A single `follow` invocation walks the immutable `flake.lock` exactly
    /// once. `crane.nixpkgs` is a separate `nixpkgs` copy, so the planner
    /// emits `crane.nixpkgs -> nixpkgs` and the full apply path runs,
    /// exercising discovery, lock-drift validation, and the per-step
    /// speculative checks from one shared lock walk.
    #[test]
    fn follow_walks_lockfile_once_per_invocation() {
        let flake = r#"{
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs";
    crane.url = "github:ipetkov/crane";
  };
  outputs = { ... }: { };
}
"#;
        let lock = r#"{
  "nodes": {
    "nixpkgs": {
      "locked": { "lastModified": 1, "narHash": "", "owner": "nixos", "repo": "nixpkgs", "rev": "aaa", "type": "github" },
      "original": { "owner": "nixos", "repo": "nixpkgs", "type": "github" }
    },
    "nixpkgs_2": {
      "locked": { "lastModified": 1, "narHash": "", "owner": "nixos", "repo": "nixpkgs", "rev": "bbb", "type": "github" },
      "original": { "owner": "nixos", "repo": "nixpkgs", "type": "github" }
    },
    "crane": {
      "inputs": { "nixpkgs": "nixpkgs_2" },
      "locked": { "lastModified": 1, "narHash": "", "owner": "ipetkov", "repo": "crane", "rev": "ccc", "type": "github" },
      "original": { "owner": "ipetkov", "repo": "crane", "type": "github" }
    },
    "root": {
      "inputs": { "nixpkgs": "nixpkgs", "crane": "crane" }
    }
  },
  "root": "root",
  "version": 7
}"#;

        crate::lock::NESTED_INPUTS_CALLS.with(|c| c.set(0));
        let out = run_in_memory(flake, lock, &FollowConfig::default()).expect("run_in_memory");
        let walks = crate::lock::NESTED_INPUTS_CALLS.with(|c| c.get());

        assert!(
            out.is_some(),
            "fixture must produce a dedup so the apply path runs; got no change",
        );
        assert_eq!(
            walks, 1,
            "nested_inputs must be walked exactly once per follow invocation, got {walks}",
        );
    }

    fn nested_input(path: &str, follows: Option<&str>, url: Option<&str>) -> NestedInput {
        NestedInput {
            path: ap(path),
            follows: follows.map(ap),
            url: url.map(ToOwned::to_owned),
        }
    }

    struct CtxFixture<'a> {
        inputs: InputMap,
        graph: FollowsGraph,
        existing_follows: HashSet<AttrPath>,
        follow_config: FollowConfig,
        nested_inputs: &'a [NestedInput],
        top_level_inputs: HashSet<String>,
        max_depth: Option<usize>,
        transitive_min: usize,
    }

    impl<'a> CtxFixture<'a> {
        fn new(nested_inputs: &'a [NestedInput], top_level: &[&str]) -> Self {
            Self {
                inputs: InputMap::new(),
                graph: FollowsGraph::default(),
                existing_follows: HashSet::new(),
                follow_config: FollowConfig::default(),
                nested_inputs,
                top_level_inputs: top_level.iter().map(|s| (*s).to_string()).collect(),
                max_depth: Some(1),
                transitive_min: 2,
            }
        }

        fn ctx(&self) -> AnalysisCtx<'_> {
            AnalysisCtx {
                nested_inputs: self.nested_inputs,
                top_level_inputs: self.top_level_inputs.clone(),
                inputs: &self.inputs,
                graph: &self.graph,
                existing_follows: &self.existing_follows,
                follow_config: &self.follow_config,
                max_depth: self.max_depth,
                transitive_min: self.transitive_min,
            }
        }
    }

    #[test]
    fn resolve_direct_candidate_returns_target_when_eligible() {
        let nested = vec![nested_input("home-manager.nixpkgs", None, None)];
        let mut fx = CtxFixture::new(&nested, &["nixpkgs", "home-manager"]);
        fx.inputs = make_input_map(vec![Input::new(seg("nixpkgs"))]);

        let result = resolve_direct_candidate(&fx.ctx(), &nested[0], &[]);

        assert_eq!(result, Some(ap("nixpkgs")));
    }

    #[test]
    fn resolve_direct_candidate_skips_when_no_top_level_match() {
        let nested = vec![nested_input("home-manager.nixpkgs", None, None)];
        let fx = CtxFixture::new(&nested, &["home-manager"]);

        let result = resolve_direct_candidate(&fx.ctx(), &nested[0], &[]);

        assert_eq!(result, None);
    }

    #[test]
    fn resolve_transitive_candidate_returns_target_when_eligible() {
        // The self-follow guard compares the target's trailing
        // segment to the nested input's leaf name, so the fixture
        // chooses `top.bar` against `parent.foo` to keep them
        // distinct.
        let nested = vec![nested_input("parent.foo", Some("top.bar"), None)];
        let fx = CtxFixture::new(&nested, &["parent", "top"]);
        let plan = FollowPlan::default();

        let result = resolve_transitive_candidate(&fx.ctx(), &plan, &nested[0]);

        assert_eq!(result, Some(("foo".to_string(), ap("top.bar"))));
    }

    #[test]
    fn resolve_transitive_candidate_skips_self_follow() {
        // Promoting `parent.nixpkgs -> other.nixpkgs` into a group
        // would amount to a no-op rename of the trailing segment, so
        // the helper rejects it even though the parents differ.
        let nested = vec![nested_input("parent.nixpkgs", Some("other.nixpkgs"), None)];
        let fx = CtxFixture::new(&nested, &["parent", "other"]);
        let plan = FollowPlan::default();

        let result = resolve_transitive_candidate(&fx.ctx(), &plan, &nested[0]);

        assert_eq!(result, None);
    }

    #[test]
    fn resolve_transitive_candidate_skips_when_already_seen() {
        // The grouping pass shares `seen_nested` with the direct
        // promotion pass; a path another phase already claimed must
        // not produce a second group entry here.
        let nested = vec![nested_input(
            "parent.flake-utils",
            Some("top.flake-utils"),
            None,
        )];
        let fx = CtxFixture::new(&nested, &["parent", "top"]);
        let plan = FollowPlan {
            seen_nested: std::iter::once(ap("parent.flake-utils")).collect(),
            ..FollowPlan::default()
        };

        let result = resolve_transitive_candidate(&fx.ctx(), &plan, &nested[0]);

        assert_eq!(result, None);
    }

    #[test]
    fn record_direct_promotion_entries_pushes_orphan_paths() {
        let entries: Vec<(AttrPath, Option<String>)> = vec![
            (ap("crane.flake-utils"), None),
            (ap("treefmt.flake-utils"), None),
        ];
        let target = ap("flake-utils");
        let mut plan = FollowPlan::default();

        record_direct_promotion_entries(&mut plan, &target, "flake-utils", &entries);

        assert_eq!(
            plan.to_follow,
            vec![
                (ap("crane.flake-utils"), ap("flake-utils")),
                (ap("treefmt.flake-utils"), ap("flake-utils")),
            ],
        );
        assert!(plan.seen_nested.contains(&ap("crane.flake-utils")));
        assert!(plan.seen_nested.contains(&ap("treefmt.flake-utils")));
    }

    #[test]
    fn record_direct_promotion_entries_skips_descendants_covered_by_ancestor() {
        let entries: Vec<(AttrPath, Option<String>)> = vec![
            (ap("crane.flake-utils"), None),
            (ap("crane.flake-utils.nested"), None),
        ];
        let target = ap("flake-utils");
        let mut plan = FollowPlan::default();

        record_direct_promotion_entries(&mut plan, &target, "flake-utils", &entries);

        assert_eq!(
            plan.to_follow,
            vec![(ap("crane.flake-utils"), ap("flake-utils"))],
        );
        assert!(
            plan.seen_nested.contains(&ap("crane.flake-utils.nested")),
            "ancestor-covered descendant must still claim seen_nested",
        );
    }

    #[test]
    fn resolve_direct_candidate_skips_existing_follow() {
        let nested = vec![nested_input("home-manager.nixpkgs", None, None)];
        let mut fx = CtxFixture::new(&nested, &["nixpkgs", "home-manager"]);
        fx.inputs = make_input_map(vec![Input::new(seg("nixpkgs"))]);
        fx.existing_follows = std::iter::once(ap("home-manager.nixpkgs")).collect();

        let result = resolve_direct_candidate(&fx.ctx(), &nested[0], &[]);

        assert_eq!(result, None);
    }

    fn fresh_state(text: &str) -> PlanState {
        let parsed = validate::ParsedSource::new(text);
        assert!(
            parsed.parse_errors.is_empty(),
            "test fixture must parse cleanly, got: {:?}",
            parsed.parse_errors,
        );
        PlanState {
            current_text: text.to_owned(),
            current_parsed: parsed,
            warnings: Vec::new(),
        }
    }

    #[test]
    fn apply_toplevel_adds_inserts_new_input() {
        let original = r#"{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
  };
  outputs = _: { };
}
"#;
        let plan = FollowPlan {
            toplevel_adds: vec![(
                "flake-utils".to_string(),
                "github:numtide/flake-utils".to_string(),
            )],
            ..FollowPlan::default()
        };
        let mut state = fresh_state(original);

        apply_toplevel_adds(&plan, &mut state, None);

        assert!(
            state
                .current_text
                .contains(r#"flake-utils.url = "github:numtide/flake-utils""#),
            "added input declaration must appear verbatim, got:\n{}",
            state.current_text,
        );
    }

    #[test]
    fn apply_follow_changes_records_accepted() {
        let original = r#"{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    home-manager.url = "github:nix-community/home-manager";
  };
  outputs = _: { };
}
"#;
        let plan = FollowPlan {
            to_follow: vec![(ap("home-manager.nixpkgs"), ap("nixpkgs"))],
            ..FollowPlan::default()
        };
        let mut state = fresh_state(original);

        let applied = apply_follow_changes(&plan, &mut state, None);

        assert_eq!(
            applied,
            vec![(ap("home-manager.nixpkgs"), ap("nixpkgs"))],
            "happy-path follow must be recorded as applied",
        );
        assert!(
            state
                .current_text
                .contains("home-manager.inputs.nixpkgs.follows = \"nixpkgs\""),
            "follows declaration must be written, got:\n{}",
            state.current_text,
        );
    }

    #[test]
    fn apply_follow_changes_skips_no_op_text() {
        // Source already contains the exact declaration the plan asks
        // for. The phase must distinguish "applied a fresh follows"
        // from "the follows was already in place" so the success
        // summary does not double-count.
        let original = r#"{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
  };
  outputs = _: { };
}
"#;
        let plan = FollowPlan {
            to_follow: vec![(ap("home-manager.nixpkgs"), ap("nixpkgs"))],
            ..FollowPlan::default()
        };
        let mut state = fresh_state(original);

        let applied = apply_follow_changes(&plan, &mut state, None);

        assert!(
            applied.is_empty(),
            "no-op follow must not be recorded, got: {applied:?}",
        );
        assert_eq!(
            state.current_text, original,
            "current_text must be byte-equal to original on no-op",
        );
    }

    #[test]
    fn apply_unfollow_changes_removes_stale() {
        let original = r#"{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
    home-manager.url = "github:nix-community/home-manager";
    home-manager.inputs.nixpkgs.follows = "nixpkgs";
  };
  outputs = _: { };
}
"#;
        let plan = FollowPlan {
            to_unfollow: vec![ap("home-manager.nixpkgs")],
            ..FollowPlan::default()
        };
        let mut state = fresh_state(original);

        let unfollowed = apply_unfollow_changes(&plan, &mut state, None);

        assert_eq!(
            unfollowed,
            vec![ap("home-manager.nixpkgs")],
            "removed path must be reported",
        );
        assert!(
            !state
                .current_text
                .contains("home-manager.inputs.nixpkgs.follows"),
            "stale follows line must be gone, got:\n{}",
            state.current_text,
        );
    }

    #[test]
    fn apply_unfollow_changes_skips_missing_path() {
        // `to_unfollow` is seeded from a graph view that may disagree
        // with the on-disk text after earlier phases mutate it. The
        // phase must drop entries whose source no longer resolves, so
        // the success summary cannot grow phantom "removed" lines.
        let original = r#"{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
  };
  outputs = _: { };
}
"#;
        let plan = FollowPlan {
            to_unfollow: vec![ap("does-not-exist")],
            ..FollowPlan::default()
        };
        let mut state = fresh_state(original);

        let unfollowed = apply_unfollow_changes(&plan, &mut state, None);

        assert!(
            unfollowed.is_empty(),
            "missing path must not be reported as removed, got: {unfollowed:?}",
        );
        assert_eq!(
            state.current_text, original,
            "current_text must be byte-equal when no path was removed",
        );
    }

    #[test]
    fn try_apply_one_leaves_state_on_no_text() {
        // The helper updates `current_text` and `current_parsed`
        // together, so a non-Accepted outcome must touch neither.
        // Otherwise the next phase would walk a parse that no longer
        // describes the text it sees.
        let original = r#"{
  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs";
  };
  outputs = _: { };
}
"#;
        let mut state = fresh_state(original);
        let change = Change::Remove {
            ids: vec![ChangeId::new(ap("does-not-exist"))],
        };

        let outcome = state.try_apply_one(change, None);

        assert!(
            matches!(outcome, StepOutcome::NoText),
            "expected NoText for a remove against an absent source",
        );
        assert_eq!(
            state.current_text, original,
            "state must be untouched on a non-Accepted outcome",
        );
    }
}