shepherd-cli 6.7.0

The canonical shepherd command-line interface over the per-project registry, run artifacts, and sprint pipeline.
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
use serde_json::{Value, json};
use std::{
    fs,
    path::{Path, PathBuf},
    process::{Command, Output},
};

struct Fixture {
    _guard: tempfile::TempDir,
    root: PathBuf,
}

impl Fixture {
    fn path(&self) -> &Path {
        &self.root
    }
}

fn fixture() -> Fixture {
    let guard = tempfile::Builder::new()
        .prefix("shepherd-run-adoption-")
        .tempdir()
        .expect("create fixture root");
    let root = fs::canonicalize(guard.path()).unwrap();
    assert!(
        Command::new("git")
            .args(["init", "--quiet"])
            .current_dir(&root)
            .status()
            .unwrap()
            .success()
    );
    fs::create_dir_all(root.join(".shepherd/runs/v657/dispatch")).unwrap();
    fs::write(
        root.join(".shepherd/runs/v657/run.json"),
        serde_json::to_vec(&json!({
            "schema_version": 1, "run": "v657", "kind": "patch-arc", "branch": "v6.5.7",
            "base": "main", "seed": "seed.md", "plan": "", "status": "planted", "lanes": [],
            "updated_at": 42, "operator_note": {"preserve": true}
        }))
        .unwrap(),
    )
    .unwrap();
    fs::write(
        root.join(".shepherd/runs/v657/seed.md"),
        b"legacy seed, preserve exactly\n",
    )
    .unwrap();
    Fixture {
        _guard: guard,
        root,
    }
}

fn invoke(root: &Path, args: &[&str]) -> Output {
    Command::new(env!("CARGO_BIN_EXE_shepherd"))
        .args(args)
        .current_dir(root)
        .env("SHEPHERD_HOME", root.join("isolated-home"))
        .output()
        .unwrap()
}

fn git(root: &Path, args: &[&str]) -> String {
    let output = Command::new("git")
        .args(args)
        .current_dir(root)
        .output()
        .expect("run git fixture command");
    assert!(
        output.status.success(),
        "git {args:?} failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    String::from_utf8_lossy(&output.stdout).trim().to_owned()
}

fn commit_all(root: &Path, message: &str) -> String {
    git(root, &["add", "--all"]);
    git(
        root,
        &[
            "-c",
            "user.name=Shepherd Tests",
            "-c",
            "user.email=shepherd-tests@example.invalid",
            "commit",
            "--quiet",
            "-m",
            message,
        ],
    );
    git(root, &["rev-parse", "HEAD"])
}

struct SuccessorFixture {
    _guard: tempfile::TempDir,
    root: PathBuf,
    source_incarnation: String,
    source_baseline: String,
    target_baseline: String,
    source_state: Vec<u8>,
    source_seed: Vec<u8>,
    source_plan: Vec<u8>,
    source_dispatch: Vec<u8>,
}

impl SuccessorFixture {
    fn new() -> Self {
        let guard = tempfile::Builder::new()
            .prefix("shepherd-run-successor-")
            .tempdir()
            .expect("create successor fixture root");
        let root = fs::canonicalize(guard.path()).expect("canonical successor fixture root");
        assert!(
            Command::new("git")
                .args(["init", "--quiet", "--initial-branch=main"])
                .current_dir(&root)
                .status()
                .expect("initialize successor fixture git")
                .success()
        );
        git(&root, &["config", "core.autocrlf", "false"]);
        fs::write(root.join("product.txt"), b"baseline product\n").expect("baseline product");
        let source_baseline = commit_all(&root, "baseline");
        git(&root, &["switch", "--quiet", "-c", "v6.7.0"]);

        let init = invoke(
            &root,
            &[
                "run",
                "init",
                "v670",
                "--kind",
                "sprint",
                "--branch",
                "v6.7.0",
                "--base",
                "main",
                "--version",
                "6.7.0",
            ],
        );
        assert!(
            init.status.success(),
            "run init failed: {}",
            String::from_utf8_lossy(&init.stderr)
        );
        let run_dir = root.join(".shepherd/runs/v670");
        fs::write(run_dir.join("seed.md"), b"source seed evidence\n").expect("source seed");
        fs::write(run_dir.join("plan.md"), b"source plan evidence\n").expect("source plan");
        fs::write(run_dir.join("phase0.md"), b"source phase zero evidence\n")
            .expect("source phase zero");
        fs::write(
            run_dir.join("plan-probes.json"),
            serde_json::to_vec_pretty(&json!({
                "schema": "shepherd.plan-probes/1",
                "baseline": source_baseline,
                "worktree_identity": "historical-source"
            }))
            .expect("encode source probes"),
        )
        .expect("source probes");
        fs::write(
            run_dir.join("dispatch/source-evidence.json"),
            b"source dispatch evidence\n",
        )
        .expect("source dispatch evidence");
        let mut state: Value = serde_json::from_slice(
            &fs::read(run_dir.join("run.json")).expect("read initialized state"),
        )
        .expect("initialized state JSON");
        state["seed"] = json!(".shepherd/runs/v670/seed.md");
        state["plan"] = json!(".shepherd/runs/v670/plan.md");
        fs::write(
            run_dir.join("run.json"),
            serde_json::to_vec_pretty(&state).expect("encode source state"),
        )
        .expect("source state");
        let source_incarnation = state["run_incarnation"]
            .as_str()
            .expect("source incarnation")
            .to_owned();

        fs::write(root.join("product.txt"), b"descendant production change\n")
            .expect("descendant product");
        let target_baseline = commit_all(&root, "production descendant");
        let source_state = fs::read(run_dir.join("run.json")).expect("source state bytes");
        let source_seed = fs::read(run_dir.join("seed.md")).expect("source seed bytes");
        let source_plan = fs::read(run_dir.join("plan.md")).expect("source plan bytes");
        let source_dispatch =
            fs::read(run_dir.join("dispatch/source-evidence.json")).expect("source dispatch bytes");
        Self {
            _guard: guard,
            root,
            source_incarnation,
            source_baseline,
            target_baseline,
            source_state,
            source_seed,
            source_plan,
            source_dispatch,
        }
    }

    fn args(&self) -> Vec<String> {
        vec![
            "run".into(),
            "successor".into(),
            "v670".into(),
            "--source-incarnation".into(),
            self.source_incarnation.clone(),
            "--version".into(),
            "6.7.0".into(),
            "--branch".into(),
            "v6.7.0".into(),
            "--base".into(),
            "main".into(),
            "--source-baseline".into(),
            self.source_baseline.clone(),
            "--baseline".into(),
            self.target_baseline.clone(),
            "--worktree".into(),
            self.root.display().to_string(),
            "--confirm".into(),
            "--json".into(),
        ]
    }

    fn invoke(&self, args: &[String]) -> Output {
        Command::new(env!("CARGO_BIN_EXE_shepherd"))
            .args(args)
            .current_dir(&self.root)
            .env("SHEPHERD_HOME", self.root.join("isolated-home"))
            .output()
            .expect("invoke successor command")
    }
}

fn state(root: &Path) -> Value {
    serde_json::from_slice(&fs::read(root.join(".shepherd/runs/v657/run.json")).unwrap()).unwrap()
}

fn registry(root: &Path) -> Value {
    serde_json::from_slice(
        &fs::read(root.join(".shepherd/native-orientation-registry.json")).unwrap(),
    )
    .unwrap()
}

#[test]
fn explicit_legacy_adoption_preserves_artifacts_and_replays_one_native_identity() {
    let temp = fixture();
    let root = temp.path();
    let original = state(root);
    let seed = fs::read(root.join(".shepherd/runs/v657/seed.md")).unwrap();
    let first = invoke(root, &["run", "migrate", "v657", "--adopt-native"]);
    assert!(
        first.status.success(),
        "{}",
        String::from_utf8_lossy(&first.stderr)
    );
    let adopted = state(root);
    assert_eq!(adopted["status"], "planted");
    assert_eq!(adopted["operator_note"], original["operator_note"]);
    assert_eq!(adopted["updated_at"], original["updated_at"]);
    assert_eq!(adopted["seed"], original["seed"]);
    assert_eq!(adopted["orientation_epoch"], 0);
    assert_eq!(adopted["run_incarnation"].as_str().unwrap().len(), 32);
    let native = registry(root);
    assert_eq!(
        native["runs"]["v657"]["run_incarnation"],
        adopted["run_incarnation"]
    );
    assert_eq!(
        native["runs"]["v657"]["legacy_adoption_sha256"]
            .as_str()
            .unwrap()
            .len(),
        64
    );
    assert!(native["runs"]["v657"]["pre"].is_null());
    assert!(native["runs"]["v657"]["post"].is_null());
    assert_eq!(
        seed,
        fs::read(root.join(".shepherd/runs/v657/seed.md")).unwrap()
    );
    let second = invoke(root, &["run", "migrate", "v657", "--adopt-native"]);
    assert!(
        second.status.success(),
        "{}",
        String::from_utf8_lossy(&second.stderr)
    );
    assert_eq!(adopted, state(root));
    assert_eq!(native, registry(root));
}

#[test]
fn adoption_cannot_import_execution_checkpoints_or_child_authority() {
    for (field, value) in [
        ("status", json!("executing")),
        ("orientation_epoch", json!(1)),
        ("run_incarnation", json!("caller-chosen-incarnation")),
        ("lanes", json!([{"id":"old", "state":"pending"}])),
    ] {
        let temp = fixture();
        let mut document = state(temp.path());
        document[field] = value;
        let path = temp.path().join(".shepherd/runs/v657/run.json");
        fs::write(&path, serde_json::to_vec(&document).unwrap()).unwrap();
        let before = fs::read(&path).unwrap();
        let result = invoke(temp.path(), &["run", "migrate", "v657", "--adopt-native"]);
        assert!(!result.status.success(), "accepted {field}");
        assert_eq!(before, fs::read(path).unwrap());
        assert!(
            !temp
                .path()
                .join(".shepherd/native-orientation-registry.json")
                .exists()
        );
    }
    for name in ["child.json", "pending-invalid.json"] {
        let temp = fixture();
        fs::write(
            temp.path().join(".shepherd/runs/v657/dispatch").join(name),
            b"{}",
        )
        .unwrap();
        let before = state(temp.path());
        let result = invoke(temp.path(), &["run", "migrate", "v657", "--adopt-native"]);
        assert!(!result.status.success(), "accepted {name}");
        assert_eq!(before, state(temp.path()));
        assert!(
            !temp
                .path()
                .join(".shepherd/native-orientation-registry.json")
                .exists()
        );
    }
}

#[test]
fn ordinary_migration_does_not_adopt_and_bulk_adoption_is_refused() {
    let temp = fixture();
    let result = invoke(temp.path(), &["run", "migrate", "v657"]);
    assert!(
        result.status.success(),
        "{}",
        String::from_utf8_lossy(&result.stderr)
    );
    assert_eq!(state(temp.path())["run_incarnation"], "");
    assert!(
        !temp
            .path()
            .join(".shepherd/native-orientation-registry.json")
            .exists()
    );
    let before = state(temp.path());
    let bulk = invoke(temp.path(), &["run", "migrate", "--all", "--adopt-native"]);
    assert!(!bulk.status.success());
    assert_eq!(before, state(temp.path()));
    assert!(
        !temp
            .path()
            .join(".shepherd/native-orientation-registry.json")
            .exists()
    );
}

#[test]
fn native_adoption_intent_cannot_be_reused_for_changed_or_unrelated_authority() {
    let temp = fixture();
    let result = invoke(temp.path(), &["run", "migrate", "v657", "--adopt-native"]);
    assert!(
        result.status.success(),
        "{}",
        String::from_utf8_lossy(&result.stderr)
    );
    let before_native = registry(temp.path());
    let path = temp.path().join(".shepherd/runs/v657/run.json");
    let mut changed = state(temp.path());
    changed["run_incarnation"] = json!("");
    changed["branch"] = json!("different-source");
    fs::write(&path, serde_json::to_vec(&changed).unwrap()).unwrap();
    let result = invoke(temp.path(), &["run", "migrate", "v657", "--adopt-native"]);
    assert!(!result.status.success());
    assert_eq!(changed, state(temp.path()));
    assert_eq!(before_native, registry(temp.path()));
    let mut unrelated = before_native.clone();
    unrelated["runs"]["v657"]
        .as_object_mut()
        .unwrap()
        .remove("legacy_adoption_sha256");
    let registry_path = temp
        .path()
        .join(".shepherd/native-orientation-registry.json");
    fs::write(&registry_path, serde_json::to_vec(&unrelated).unwrap()).unwrap();
    let result = invoke(temp.path(), &["run", "migrate", "v657", "--adopt-native"]);
    assert!(!result.status.success());
    assert_eq!(unrelated, registry(temp.path()));
}

#[test]
fn run_successor_archives_the_exact_source_and_mints_one_fresh_incarnation() {
    let fixture = SuccessorFixture::new();
    let first = fixture.invoke(&fixture.args());
    assert!(
        first.status.success(),
        "successor failed: {}",
        String::from_utf8_lossy(&first.stderr)
    );
    let report: Value = serde_json::from_slice(&first.stdout).expect("successor report JSON");
    assert_eq!(report["schema"], "shepherd.run-successor/1");
    assert_eq!(report["run"], "v670");
    assert_eq!(report["source_incarnation"], fixture.source_incarnation);
    let target_incarnation = report["target_incarnation"]
        .as_str()
        .expect("target incarnation");
    assert_eq!(target_incarnation.len(), 32);
    assert_ne!(target_incarnation, fixture.source_incarnation);
    assert_eq!(report["version"], "6.7.0");
    assert_eq!(report["branch"], "v6.7.0");
    assert_eq!(report["base"], "main");
    assert_eq!(report["base_commit"], fixture.source_baseline);
    assert_eq!(report["source_baseline"], fixture.source_baseline);
    assert_eq!(report["baseline"], fixture.target_baseline);

    let active: Value = serde_json::from_slice(
        &fs::read(fixture.root.join(".shepherd/runs/v670/run.json")).expect("successor run state"),
    )
    .expect("successor run JSON");
    assert_eq!(active["run"], "v670");
    assert_eq!(active["run_incarnation"], target_incarnation);
    assert_eq!(active["status"], "planted");
    assert_eq!(active["orientation_epoch"], 0);
    assert_eq!(active["branch"], "v6.7.0");
    assert_eq!(active["base"], "main");
    assert_eq!(active["seed"], "");
    assert_eq!(active["plan"], "");
    assert_eq!(active["lanes"], json!([]));

    let archive = PathBuf::from(
        report["source_archive"]
            .as_str()
            .expect("source archive path"),
    );
    assert_eq!(
        fixture.source_state,
        fs::read(archive.join("run.json")).expect("archived source state")
    );
    assert_eq!(
        fixture.source_seed,
        fs::read(archive.join("seed.md")).expect("archived source seed")
    );
    assert_eq!(
        fixture.source_plan,
        fs::read(archive.join("plan.md")).expect("archived source plan")
    );
    assert_eq!(
        fixture.source_dispatch,
        fs::read(archive.join("dispatch/source-evidence.json"))
            .expect("archived dispatch evidence")
    );
    assert!(fixture.root.join(".shepherd/runs/v670/dispatch").is_dir());
    assert!(
        fs::read_dir(fixture.root.join(".shepherd/runs/v670/dispatch"))
            .expect("fresh successor dispatch directory")
            .next()
            .is_none(),
        "source dispatch authority must not enter the successor"
    );
    for stale in ["seed.md", "plan.md", "phase0.md", "plan-probes.json"] {
        assert!(
            !fixture
                .root
                .join(".shepherd/runs/v670")
                .join(stale)
                .exists(),
            "successor must not fabricate or reuse {stale}"
        );
    }

    let native: Value = serde_json::from_slice(
        &fs::read(
            fixture
                .root
                .join(".shepherd/native-orientation-registry.json"),
        )
        .expect("native successor registry"),
    )
    .expect("native successor registry JSON");
    assert_eq!(
        native["runs"]["v670"]["run_incarnation"],
        target_incarnation
    );
    assert_eq!(
        native["runs"]["v670"]["predecessors"][0]["run_incarnation"],
        fixture.source_incarnation
    );
    assert!(
        native["pending_successors"]
            .as_object()
            .is_some_and(|v| v.is_empty())
    );

    let fresh_seed = include_str!("fixtures/seed-contract/valid/seed.md").replace("v999", "v670");
    fs::write(fixture.root.join(".shepherd/runs/v670/seed.md"), fresh_seed)
        .expect("fresh successor seed");
    fs::write(
        fixture.root.join(".shepherd/runs/v670/mesh.md"),
        include_str!("fixtures/seed-contract/valid/mesh.md"),
    )
    .expect("fresh successor mesh");
    let plant = invoke(
        &fixture.root,
        &[
            "run",
            "set",
            "v670",
            "--seed",
            ".shepherd/runs/v670/seed.md",
        ],
    );
    assert!(
        plant.status.success(),
        "ordinary successor planting failed: {}",
        String::from_utf8_lossy(&plant.stderr)
    );
    let seed = invoke(
        &fixture.root,
        &["seed", "verify", ".shepherd/runs/v670/seed.md"],
    );
    assert!(
        seed.status.success(),
        "ordinary successor seed verification failed: {}",
        String::from_utf8_lossy(&seed.stderr)
    );

    fs::write(archive.join("plan.md"), b"tampered historical plan\n")
        .expect("tamper archived plan control");
    let tampered = fixture.invoke(&fixture.args());
    assert!(
        !tampered.status.success(),
        "tampered source archive replayed"
    );
    assert!(
        String::from_utf8_lossy(&tampered.stderr).contains("archived source evidence changed"),
        "archive tamper lacked causal diagnostic: {}",
        String::from_utf8_lossy(&tampered.stderr)
    );
    fs::write(archive.join("plan.md"), &fixture.source_plan)
        .expect("restore archived plan control");

    let second = fixture.invoke(&fixture.args());
    assert!(
        second.status.success(),
        "exact successor replay failed: {}",
        String::from_utf8_lossy(&second.stderr)
    );
    let replay: Value = serde_json::from_slice(&second.stdout).expect("successor replay JSON");
    assert_eq!(replay["target_incarnation"], target_incarnation);
    assert_eq!(replay["source_archive"], report["source_archive"]);
    let replay_native: Value = serde_json::from_slice(
        &fs::read(
            fixture
                .root
                .join(".shepherd/native-orientation-registry.json"),
        )
        .expect("replayed native successor registry"),
    )
    .expect("replayed native successor registry JSON");
    assert_eq!(
        replay_native, native,
        "replay must mint no second successor"
    );
}

#[test]
fn run_successor_rejects_every_identity_mismatch_without_mutation() {
    let cases = [
        (
            "--source-incarnation",
            "00000000000000000000000000000000",
            "source incarnation",
        ),
        ("--version", "6.7.1", "version"),
        ("--branch", "wrong-branch", "branch"),
        ("--base", "wrong-base", "base"),
        (
            "--source-baseline",
            "1111111111111111111111111111111111111111",
            "source baseline",
        ),
        (
            "--baseline",
            "2222222222222222222222222222222222222222",
            "baseline",
        ),
    ];
    for (flag, replacement, expected_diagnostic) in cases {
        let fixture = SuccessorFixture::new();
        let mut args = fixture.args();
        let index = args.iter().position(|value| value == flag).expect("flag");
        args[index + 1] = replacement.into();
        let registry_before = fs::read(
            fixture
                .root
                .join(".shepherd/native-orientation-registry.json"),
        )
        .expect("registry before denial");
        let result = fixture.invoke(&args);
        assert!(
            !result.status.success(),
            "identity mismatch {flag} unexpectedly succeeded"
        );
        assert!(
            String::from_utf8_lossy(&result.stderr).contains(expected_diagnostic),
            "identity mismatch {flag} lacked its causal diagnostic: {}",
            String::from_utf8_lossy(&result.stderr)
        );
        assert_eq!(
            fixture.source_state,
            fs::read(fixture.root.join(".shepherd/runs/v670/run.json"))
                .expect("source state after denial"),
            "identity mismatch {flag} mutated source state"
        );
        assert_eq!(
            registry_before,
            fs::read(
                fixture
                    .root
                    .join(".shepherd/native-orientation-registry.json"),
            )
            .expect("registry after denial"),
            "identity mismatch {flag} mutated Native authority"
        );
        assert!(
            !fixture
                .root
                .join(".shepherd/runs/v670/.incarnations")
                .exists(),
            "identity mismatch {flag} created archive state"
        );
    }
}

#[test]
fn run_successor_requires_confirmation_and_exact_selected_worktree() {
    let fixture = SuccessorFixture::new();
    let mut without_confirmation = fixture.args();
    without_confirmation.retain(|value| value != "--confirm");
    let denied = fixture.invoke(&without_confirmation);
    assert!(
        !denied.status.success(),
        "successor ran without confirmation"
    );
    assert!(
        String::from_utf8_lossy(&denied.stderr).contains("--confirm"),
        "missing confirmation lacked its causal diagnostic: {}",
        String::from_utf8_lossy(&denied.stderr)
    );
    assert_eq!(
        fixture.source_state,
        fs::read(fixture.root.join(".shepherd/runs/v670/run.json")).expect("unchanged state")
    );

    let unrelated = tempfile::tempdir().expect("unrelated worktree directory");
    let mut wrong_worktree = fixture.args();
    let index = wrong_worktree
        .iter()
        .position(|value| value == "--worktree")
        .expect("worktree flag");
    wrong_worktree[index + 1] = unrelated.path().display().to_string();
    let denied = fixture.invoke(&wrong_worktree);
    assert!(!denied.status.success(), "unrelated worktree was accepted");
    assert!(
        String::from_utf8_lossy(&denied.stderr).contains("worktree"),
        "wrong worktree lacked its causal diagnostic: {}",
        String::from_utf8_lossy(&denied.stderr)
    );
    assert_eq!(
        fixture.source_state,
        fs::read(fixture.root.join(".shepherd/runs/v670/run.json")).expect("unchanged state")
    );
}

#[test]
fn run_successor_rejects_uncommitted_product_outside_run_evidence() {
    let fixture = SuccessorFixture::new();
    fs::write(
        fixture.root.join("product.txt"),
        b"uncommitted product drift\n",
    )
    .expect("dirty product control");
    let registry_before = fs::read(
        fixture
            .root
            .join(".shepherd/native-orientation-registry.json"),
    )
    .expect("registry before dirty denial");
    let denied = fixture.invoke(&fixture.args());
    assert!(
        !denied.status.success(),
        "dirty product worktree was accepted"
    );
    assert!(
        String::from_utf8_lossy(&denied.stderr).contains("uncommitted")
            && String::from_utf8_lossy(&denied.stderr).contains("product"),
        "dirty product denial lacked causal diagnostic: {}",
        String::from_utf8_lossy(&denied.stderr)
    );
    assert_eq!(
        fixture.source_state,
        fs::read(fixture.root.join(".shepherd/runs/v670/run.json")).expect("unchanged source")
    );
    assert_eq!(
        registry_before,
        fs::read(
            fixture
                .root
                .join(".shepherd/native-orientation-registry.json"),
        )
        .expect("registry after dirty denial")
    );
}

#[test]
fn run_successor_rejects_ambiguous_native_history() {
    let fixture = SuccessorFixture::new();
    let first = fixture.invoke(&fixture.args());
    assert!(
        first.status.success(),
        "initial successor failed: {}",
        String::from_utf8_lossy(&first.stderr)
    );
    let registry_path = fixture
        .root
        .join(".shepherd/native-orientation-registry.json");
    let mut native: Value =
        serde_json::from_slice(&fs::read(&registry_path).expect("native history"))
            .expect("native history JSON");
    let duplicate = native["runs"]["v670"]["predecessors"][0].clone();
    native["runs"]["v670"]["predecessors"]
        .as_array_mut()
        .expect("predecessor rows")
        .push(duplicate);
    fs::write(
        &registry_path,
        serde_json::to_vec_pretty(&native).expect("ambiguous Native history JSON"),
    )
    .expect("ambiguous Native history");
    let before = fs::read(fixture.root.join(".shepherd/runs/v670/run.json"))
        .expect("active state before ambiguity");
    let denied = fixture.invoke(&fixture.args());
    assert!(
        !denied.status.success(),
        "ambiguous Native history replayed"
    );
    assert!(
        String::from_utf8_lossy(&denied.stderr).contains("ambiguous"),
        "ambiguous history lacked causal diagnostic: {}",
        String::from_utf8_lossy(&denied.stderr)
    );
    assert_eq!(
        before,
        fs::read(fixture.root.join(".shepherd/runs/v670/run.json"))
            .expect("active state after ambiguity")
    );
}

fn source_topology(root: &Path) -> Vec<(String, bool, u64)> {
    fn visit(root: &Path, directory: &Path, rows: &mut Vec<(String, bool, u64)>) {
        let mut entries = fs::read_dir(directory)
            .expect("read topology directory")
            .collect::<Result<Vec<_>, _>>()
            .expect("read topology entries");
        entries.sort_by_key(std::fs::DirEntry::file_name);
        for entry in entries {
            let path = entry.path();
            let metadata = fs::symlink_metadata(&path).expect("topology metadata");
            let relative = path
                .strip_prefix(root)
                .expect("topology relative path")
                .to_string_lossy()
                .replace('\\', "/");
            rows.push((relative, metadata.is_dir(), metadata.len()));
            if metadata.is_dir() {
                visit(root, &path, rows);
            }
        }
    }
    let mut rows = Vec::new();
    visit(root, root, &mut rows);
    rows
}

#[test]
fn run_successor_rejects_each_archive_bound_before_intent_or_move() {
    for (case, expected) in [
        ("state-bytes", "exceeds"),
        ("probe-bytes", "exceeds"),
        ("entries", "maximum entry count"),
        ("depth", "maximum depth"),
        ("path", "maximum 512 bytes"),
        ("file-bytes", "maximum 16777216 bytes"),
        ("total-bytes", "maximum total"),
    ] {
        let fixture = SuccessorFixture::new();
        let run_dir = fixture.root.join(".shepherd/runs/v670");
        match case {
            "state-bytes" => {
                fs::OpenOptions::new()
                    .write(true)
                    .open(run_dir.join("run.json"))
                    .expect("open oversized state")
                    .set_len(1024 * 1024 + 1)
                    .expect("size oversized state");
            }
            "probe-bytes" => {
                fs::OpenOptions::new()
                    .write(true)
                    .open(run_dir.join("plan-probes.json"))
                    .expect("open oversized probes")
                    .set_len(1024 * 1024 + 1)
                    .expect("size oversized probes");
            }
            "entries" => {
                let directory = run_dir.join("entry-overflow");
                fs::create_dir(&directory).expect("entry overflow directory");
                for index in 0..4097 {
                    fs::write(directory.join(format!("entry-{index:04}")), b"")
                        .expect("entry overflow file");
                }
            }
            "depth" => {
                let mut directory = run_dir.join("depth-overflow");
                for _ in 0..17 {
                    directory.push("nested");
                }
                fs::create_dir_all(&directory).expect("depth overflow tree");
                fs::write(directory.join("evidence"), b"deep").expect("deep evidence");
            }
            "path" => {
                let segment = "p".repeat(180);
                let directory = run_dir.join(&segment).join(&segment).join(&segment);
                fs::create_dir_all(&directory).expect("path overflow tree");
                fs::write(directory.join("evidence"), b"long path").expect("long path evidence");
            }
            "file-bytes" => {
                fs::File::create(run_dir.join("oversized-evidence.bin"))
                    .expect("oversized evidence")
                    .set_len(16 * 1024 * 1024 + 1)
                    .expect("size oversized evidence");
            }
            "total-bytes" => {
                let directory = run_dir.join("total-overflow");
                fs::create_dir(&directory).expect("total overflow directory");
                for index in 0..17 {
                    fs::File::create(directory.join(format!("chunk-{index:02}.bin")))
                        .expect("aggregate evidence")
                        .set_len(16 * 1024 * 1024)
                        .expect("size aggregate evidence");
                }
            }
            _ => unreachable!(),
        }
        let state_before = fs::read(run_dir.join("run.json")).expect("state before bound denial");
        let registry_path = fixture
            .root
            .join(".shepherd/native-orientation-registry.json");
        let registry_before = fs::read(&registry_path).expect("registry before bound denial");
        let topology_before = source_topology(&run_dir);
        let denied = fixture.invoke(&fixture.args());
        assert!(
            !denied.status.success(),
            "archive bound {case} was accepted"
        );
        assert!(
            String::from_utf8_lossy(&denied.stderr).contains(expected),
            "archive bound {case} lacked `{expected}`: {}",
            String::from_utf8_lossy(&denied.stderr)
        );
        assert_eq!(
            state_before,
            fs::read(run_dir.join("run.json")).expect("state after bound denial"),
            "archive bound {case} changed source state"
        );
        assert_eq!(
            registry_before,
            fs::read(&registry_path).expect("registry after bound denial"),
            "archive bound {case} published Native intent"
        );
        assert_eq!(
            topology_before,
            source_topology(&run_dir),
            "archive bound {case} moved source topology"
        );
        assert!(
            !run_dir.join(".incarnations").exists(),
            "archive bound {case} created an archive"
        );
    }
}

#[test]
fn run_successor_rejects_oversized_pending_registry_before_archive_move() {
    let fixture = SuccessorFixture::new();
    let registry_path = fixture
        .root
        .join(".shepherd/native-orientation-registry.json");
    let mut native: Value =
        serde_json::from_slice(&fs::read(&registry_path).expect("native registry"))
            .expect("native registry JSON");
    native["runs"]["v670"]["legacy_adoption_sha256"] = json!("a".repeat(130 * 1024));
    fs::write(
        &registry_path,
        serde_json::to_vec_pretty(&native).expect("near-cap Native registry JSON"),
    )
    .expect("near-cap Native registry");
    let registry_before = fs::read(&registry_path).expect("near-cap registry bytes");
    let run_dir = fixture.root.join(".shepherd/runs/v670");
    let topology_before = source_topology(&run_dir);
    let denied = fixture.invoke(&fixture.args());
    assert!(
        !denied.status.success(),
        "near-cap registry admitted successor"
    );
    assert!(
        String::from_utf8_lossy(&denied.stderr).contains("exceeds its bounded reader"),
        "near-cap denial lacked capacity diagnostic: {}",
        String::from_utf8_lossy(&denied.stderr)
    );
    assert_eq!(
        registry_before,
        fs::read(&registry_path).expect("registry after capacity denial")
    );
    assert_eq!(topology_before, source_topology(&run_dir));
    assert!(!run_dir.join(".incarnations").exists());
}

fn grow_unrelated_native_run_near_limit(registry_path: &Path) {
    const TARGET: usize = 261_000;
    let mut native: Value =
        serde_json::from_slice(&fs::read(registry_path).expect("pending registry"))
            .expect("pending registry JSON");
    let mut unrelated = native["runs"]["v670"].clone();
    unrelated["run"] = json!("v999");
    unrelated["run_incarnation"] = json!("99999999999999999999999999999999");
    unrelated["predecessors"] = json!([]);
    unrelated["legacy_adoption_sha256"] = json!("");
    native["runs"]["v999"] = unrelated;
    let without_padding = serde_json::to_vec_pretty(&native).expect("near-limit registry shape");
    assert!(without_padding.len() < TARGET);
    native["runs"]["v999"]["legacy_adoption_sha256"] =
        json!("r".repeat(TARGET - without_padding.len()));
    let bytes = serde_json::to_vec_pretty(&native).expect("near-limit registry bytes");
    assert!(
        (260_900..262_144).contains(&bytes.len()),
        "len={}",
        bytes.len()
    );
    fs::write(registry_path, bytes).expect("near-limit unrelated run");
}

#[test]
fn run_successor_terminal_reservation_survives_near_cap_cross_run_growth() {
    for abort in [false, true] {
        let fixture = SuccessorFixture::new();
        let run_dir = fixture.root.join(".shepherd/runs/v670");
        let archive = run_dir
            .join(".incarnations")
            .join(&fixture.source_incarnation)
            .join("source");
        fs::create_dir_all(&archive).expect("near-cap collision archive");
        fs::write(archive.join("plan.md"), b"near-cap collision\n").expect("near-cap collision");
        let pending_result = fixture.invoke(&fixture.args());
        assert!(
            !pending_result.status.success(),
            "collision must leave pending"
        );
        let registry_path = fixture
            .root
            .join(".shepherd/native-orientation-registry.json");
        let pending: Value =
            serde_json::from_slice(&fs::read(&registry_path).expect("pending registry"))
                .expect("pending registry JSON");
        let target = pending["pending_successors"]["v670"]["target_incarnation"]
            .as_str()
            .expect("pending target")
            .to_owned();
        assert_eq!(
            pending["pending_successors"]["v670"]["terminal_reservation"]
                .as_str()
                .map(str::len),
            Some(512)
        );
        grow_unrelated_native_run_near_limit(&registry_path);
        if abort {
            let result = invoke(
                &fixture.root,
                &[
                    "run",
                    "successor-abort",
                    "v670",
                    "--source-incarnation",
                    &fixture.source_incarnation,
                    "--target-incarnation",
                    &target,
                    "--confirm",
                ],
            );
            assert!(
                result.status.success(),
                "near-cap abort failed: {}",
                String::from_utf8_lossy(&result.stderr)
            );
            assert_eq!(
                fixture.source_state,
                fs::read(run_dir.join("run.json")).expect("near-cap abort source")
            );
        } else {
            fs::remove_file(archive.join("plan.md")).expect("clear near-cap collision");
            let result = fixture.invoke(&fixture.args());
            assert!(
                result.status.success(),
                "near-cap completion failed: {}",
                String::from_utf8_lossy(&result.stderr)
            );
            let active: Value = serde_json::from_slice(
                &fs::read(run_dir.join("run.json")).expect("near-cap successor state"),
            )
            .expect("near-cap successor JSON");
            assert_eq!(active["run_incarnation"], target);
        }
        let terminal = fs::read(&registry_path).expect("near-cap terminal registry");
        assert!(terminal.len() <= 256 * 1024, "len={}", terminal.len());
        let terminal: Value = serde_json::from_slice(&terminal).expect("terminal registry JSON");
        assert!(
            terminal["pending_successors"]
                .as_object()
                .is_some_and(|v| v.is_empty())
        );
    }
}

#[test]
fn run_successor_replays_exact_preflight_failure_and_blocks_identity_drift() {
    let fixture = SuccessorFixture::new();
    let run_dir = fixture.root.join(".shepherd/runs/v670");
    let archive = run_dir
        .join(".incarnations")
        .join(&fixture.source_incarnation)
        .join("source");
    fs::create_dir_all(&archive).expect("create partial archive root");
    fs::write(archive.join("plan.md"), b"collision\n").expect("seed archive collision");

    let first = fixture.invoke(&fixture.args());
    assert!(
        !first.status.success(),
        "archive collision unexpectedly succeeded"
    );
    assert!(
        String::from_utf8_lossy(&first.stderr).contains("archive destination already exists"),
        "partial failure lacked causal archive diagnostic: {}",
        String::from_utf8_lossy(&first.stderr)
    );
    assert_eq!(
        fixture.source_seed,
        fs::read(run_dir.join("seed.md")).expect("source seed stayed active")
    );
    assert_eq!(
        fixture.source_plan,
        fs::read(run_dir.join("plan.md")).expect("source plan stayed active")
    );
    assert_eq!(
        fixture.source_dispatch,
        fs::read(run_dir.join("dispatch/source-evidence.json"))
            .expect("source dispatch stayed active")
    );
    let pending: Value = serde_json::from_slice(
        &fs::read(
            fixture
                .root
                .join(".shepherd/native-orientation-registry.json"),
        )
        .expect("pending Native registry"),
    )
    .expect("pending Native registry JSON");
    let target_incarnation = pending["pending_successors"]["v670"]["target_incarnation"]
        .as_str()
        .expect("durable target incarnation")
        .to_owned();

    git(
        &fixture.root,
        &["branch", "--force", "main", &fixture.target_baseline],
    );
    let moved_base = fixture.invoke(&fixture.args());
    assert!(!moved_base.status.success(), "moved base ref replayed");
    assert!(
        String::from_utf8_lossy(&moved_base.stderr).contains("base commit changed")
            && String::from_utf8_lossy(&moved_base.stderr).contains(&fixture.source_baseline)
            && String::from_utf8_lossy(&moved_base.stderr).contains(&fixture.target_baseline),
        "moved base lacked exact expected/observed commits: {}",
        String::from_utf8_lossy(&moved_base.stderr)
    );
    git(
        &fixture.root,
        &["branch", "--force", "main", &fixture.source_baseline],
    );

    let blocked = invoke(
        &fixture.root,
        &["run", "set", "v670", "--seed", "attacker.md"],
    );
    assert!(
        !blocked.status.success(),
        "mutation crossed pending successor"
    );
    assert!(
        String::from_utf8_lossy(&blocked.stderr).contains("incomplete successor transition"),
        "pending mutation denial lacked recovery route: {}",
        String::from_utf8_lossy(&blocked.stderr)
    );

    fs::remove_file(archive.join("plan.md")).expect("remove injected collision");
    let resumed = fixture.invoke(&fixture.args());
    assert!(
        resumed.status.success(),
        "exact successor resume failed: {}",
        String::from_utf8_lossy(&resumed.stderr)
    );
    let report: Value = serde_json::from_slice(&resumed.stdout).expect("resumed report JSON");
    assert_eq!(report["target_incarnation"], target_incarnation);
    assert_eq!(
        fixture.source_plan,
        fs::read(archive.join("plan.md")).expect("resumed archived source plan")
    );
    let active: Value = serde_json::from_slice(
        &fs::read(fixture.root.join(".shepherd/runs/v670/run.json")).expect("resumed active state"),
    )
    .expect("resumed active state JSON");
    assert_eq!(active["run_incarnation"], target_incarnation);
    let native: Value = serde_json::from_slice(
        &fs::read(
            fixture
                .root
                .join(".shepherd/native-orientation-registry.json"),
        )
        .expect("completed Native registry"),
    )
    .expect("completed Native registry JSON");
    assert!(
        native["pending_successors"]
            .as_object()
            .is_some_and(|v| v.is_empty())
    );
    assert_eq!(
        native["runs"]["v670"]["predecessors"]
            .as_array()
            .map(Vec::len),
        Some(1)
    );
}

#[test]
fn run_successor_abort_clears_only_exact_unactivated_pending_and_preserves_failure() {
    let fixture = SuccessorFixture::new();
    let run_dir = fixture.root.join(".shepherd/runs/v670");
    let archive = run_dir
        .join(".incarnations")
        .join(&fixture.source_incarnation)
        .join("source");
    fs::create_dir_all(&archive).expect("create collision archive");
    fs::write(archive.join("plan.md"), b"permanent collision\n")
        .expect("permanent archive collision");
    let source_before = fs::read(run_dir.join("run.json")).expect("source before abort");
    let failed = fixture.invoke(&fixture.args());
    assert!(
        !failed.status.success(),
        "permanent collision unexpectedly succeeded"
    );
    let pending: Value = serde_json::from_slice(
        &fs::read(
            fixture
                .root
                .join(".shepherd/native-orientation-registry.json"),
        )
        .expect("pending registry"),
    )
    .expect("pending registry JSON");
    let target = pending["pending_successors"]["v670"]["target_incarnation"]
        .as_str()
        .expect("pending target")
        .to_owned();

    let wrong = invoke(
        &fixture.root,
        &[
            "run",
            "successor-abort",
            "v670",
            "--source-incarnation",
            &fixture.source_incarnation,
            "--target-incarnation",
            "00000000000000000000000000000000",
            "--confirm",
        ],
    );
    assert!(
        !wrong.status.success(),
        "wrong target aborted pending intent"
    );

    let aborted = invoke(
        &fixture.root,
        &[
            "run",
            "successor-abort",
            "v670",
            "--source-incarnation",
            &fixture.source_incarnation,
            "--target-incarnation",
            &target,
            "--confirm",
            "--json",
        ],
    );
    assert!(
        aborted.status.success(),
        "exact successor abort failed: {}",
        String::from_utf8_lossy(&aborted.stderr)
    );
    let report: Value = serde_json::from_slice(&aborted.stdout).expect("abort report JSON");
    assert_eq!(report["schema"], "shepherd.run-successor-abort/1");
    assert_eq!(report["source_incarnation"], fixture.source_incarnation);
    assert_eq!(report["target_incarnation"], target);
    assert_eq!(
        source_before,
        fs::read(run_dir.join("run.json")).expect("source after abort")
    );
    assert_eq!(
        fs::read(archive.join("plan.md")).expect("preserved collision"),
        b"permanent collision\n"
    );
    let native: Value = serde_json::from_slice(
        &fs::read(
            fixture
                .root
                .join(".shepherd/native-orientation-registry.json"),
        )
        .expect("aborted registry"),
    )
    .expect("aborted registry JSON");
    assert!(
        native["pending_successors"]
            .as_object()
            .is_some_and(|v| v.is_empty())
    );
    assert_eq!(
        native["aborted_successors"]["v670"][0]["target_incarnation"],
        target
    );
    let ordinary = invoke(
        &fixture.root,
        &["run", "set", "v670", "--seed", "fresh-after-abort.md"],
    );
    assert!(
        ordinary.status.success(),
        "abort did not restore ordinary mutation: {}",
        String::from_utf8_lossy(&ordinary.stderr)
    );
}

#[test]
fn run_successor_abort_recovers_post_rotation_pre_native_completion_crash() {
    let fixture = SuccessorFixture::new();
    let run_dir = fixture.root.join(".shepherd/runs/v670");
    let archive = run_dir
        .join(".incarnations")
        .join(&fixture.source_incarnation)
        .join("source");
    fs::create_dir_all(&archive).expect("create successor archive");
    fs::write(archive.join("plan.md"), b"prepare pending\n").expect("pending collision");
    assert!(!fixture.invoke(&fixture.args()).status.success());
    let registry_path = fixture
        .root
        .join(".shepherd/native-orientation-registry.json");
    let pending_registry: Value =
        serde_json::from_slice(&fs::read(&registry_path).expect("pending registry"))
            .expect("pending registry JSON");
    let pending = pending_registry["pending_successors"]["v670"].clone();
    let target = pending["target_incarnation"]
        .as_str()
        .expect("pending target")
        .to_owned();
    fs::remove_file(archive.join("plan.md")).expect("clear pending collision");
    let completed = fixture.invoke(&fixture.args());
    assert!(
        completed.status.success(),
        "complete successor fixture: {}",
        String::from_utf8_lossy(&completed.stderr)
    );

    // Recreate the exact crash seam after filesystem rotation and before the
    // atomic Native completion write. The pending snapshot contains the exact
    // source authority that must still be current at this seam.
    let mut crashed: Value =
        serde_json::from_slice(&fs::read(&registry_path).expect("completed registry"))
            .expect("completed registry JSON");
    crashed["runs"]["v670"] = pending["source_authority"].clone();
    crashed["pending_successors"]["v670"] = pending;
    fs::write(
        &registry_path,
        serde_json::to_vec_pretty(&crashed).expect("crash-seam registry JSON"),
    )
    .expect("crash-seam registry");

    let abort_nonce = crashed["pending_successors"]["v670"]["abort_nonce"]
        .as_str()
        .expect("abort nonce");
    let partial_staging = run_dir
        .join(".incarnations")
        .join(&fixture.source_incarnation)
        .join(format!("abort-{abort_nonce}/restore-0"));
    fs::create_dir_all(&partial_staging).expect("partial owned staging");
    fs::write(partial_staging.join("run.json"), b"partial").expect("partial owned restored file");

    fs::write(run_dir.join("dispatch/activated.json"), b"activated\n")
        .expect("activated target control");
    let refused = invoke(
        &fixture.root,
        &[
            "run",
            "successor-abort",
            "v670",
            "--source-incarnation",
            &fixture.source_incarnation,
            "--target-incarnation",
            &target,
            "--confirm",
        ],
    );
    assert!(!refused.status.success(), "activated target was aborted");
    assert!(
        String::from_utf8_lossy(&refused.stderr).contains("target lanes or dispatch authority"),
        "activated-target denial lacked causal diagnostic: {}",
        String::from_utf8_lossy(&refused.stderr)
    );
    fs::remove_file(run_dir.join("dispatch/activated.json"))
        .expect("remove activated target control");

    fs::write(run_dir.join("seed.md"), b"foreign final destination\n")
        .expect("foreign final destination control");
    let foreign = invoke(
        &fixture.root,
        &[
            "run",
            "successor-abort",
            "v670",
            "--source-incarnation",
            &fixture.source_incarnation,
            "--target-incarnation",
            &target,
            "--confirm",
        ],
    );
    assert!(
        !foreign.status.success(),
        "foreign final destination was overwritten"
    );
    assert!(
        String::from_utf8_lossy(&foreign.stderr).contains("unowned target entry"),
        "foreign destination lacked causal refusal: {}",
        String::from_utf8_lossy(&foreign.stderr)
    );
    assert_eq!(
        fs::read(run_dir.join("seed.md")).expect("preserved foreign destination"),
        b"foreign final destination\n"
    );
    fs::remove_file(run_dir.join("seed.md")).expect("remove foreign destination control");

    let aborted = invoke(
        &fixture.root,
        &[
            "run",
            "successor-abort",
            "v670",
            "--source-incarnation",
            &fixture.source_incarnation,
            "--target-incarnation",
            &target,
            "--confirm",
        ],
    );
    assert!(
        aborted.status.success(),
        "post-rotation abort failed: {}",
        String::from_utf8_lossy(&aborted.stderr)
    );
    assert_eq!(
        fixture.source_state,
        fs::read(run_dir.join("run.json")).expect("restored source state")
    );
    assert_eq!(
        fixture.source_seed,
        fs::read(run_dir.join("seed.md")).expect("restored source seed")
    );
    assert_eq!(
        fixture.source_plan,
        fs::read(run_dir.join("plan.md")).expect("restored source plan")
    );
    assert_eq!(
        fixture.source_state,
        fs::read(archive.join("run.json")).expect("preserved archived source state")
    );
    let failed_target = run_dir
        .join(".incarnations")
        .join(&fixture.source_incarnation)
        .join("failed-targets")
        .join(&target);
    let failed_state: Value = serde_json::from_slice(
        &fs::read(failed_target.join("run.json")).expect("failed target state"),
    )
    .expect("failed target state JSON");
    assert_eq!(failed_state["run_incarnation"], target);
    assert_eq!(
        fs::read(partial_staging.join("run.json")).expect("preserved partial staging"),
        b"partial"
    );
    assert!(
        fs::read_dir(failed_target.join("dispatch"))
            .expect("failed target dispatch")
            .next()
            .is_none()
    );
    let native: Value =
        serde_json::from_slice(&fs::read(&registry_path).expect("aborted crash-seam registry"))
            .expect("aborted crash-seam registry JSON");
    assert!(
        native["pending_successors"]
            .as_object()
            .is_some_and(|v| v.is_empty())
    );
    assert_eq!(
        native["runs"]["v670"]["run_incarnation"],
        fixture.source_incarnation
    );
}