blyte 0.78.2

The blyte coding-agent harness: headless runs with a sealed verification gate, and a REPL.
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
use std::path::Path;
use std::process::Command;

fn git(dir: &Path, args: &[&str]) -> String {
    let out = Command::new("git")
        .args(["-c", "user.name=test", "-c", "user.email=test@test.com"])
        .args(args)
        .current_dir(dir)
        .output()
        .unwrap();
    assert!(
        out.status.success(),
        "git {args:?} failed: {}",
        String::from_utf8_lossy(&out.stderr)
    );
    String::from_utf8_lossy(&out.stdout).trim().to_string()
}

fn write_file(p: &Path, text: &str) {
    std::fs::create_dir_all(p.parent().unwrap()).unwrap();
    std::fs::write(p, text).unwrap();
}

fn init_repo() -> tempfile::TempDir {
    let t = tempfile::tempdir().unwrap();
    git(t.path(), &["init", "-q", "-b", "main"]);
    write_file(&t.path().join("README.md"), "initial\n");
    git(t.path(), &["add", "."]);
    git(t.path(), &["commit", "-qm", "initial", "--no-gpg-sign"]);
    t
}

fn blyte_output(cwd: &Path, args: &[&str]) -> std::process::Output {
    let env_dir = tempfile::tempdir().unwrap();
    Command::new(env!("CARGO_BIN_EXE_blyte"))
        .args(args)
        .current_dir(cwd)
        .env("BLYTE_NO_UPDATE_CHECK", "1")
        .env("BLYTE_ENV_FILE", "")
        .env("BLYTE_STATE_DIR", env_dir.path().join("state"))
        .env("BLYTE_GATE_SLOT_DIR", env_dir.path().join("slots"))
        .env("HOME", env_dir.path().join("home"))
        .env("XDG_CONFIG_HOME", env_dir.path().join("home/.config"))
        .env_remove("GEMINI_API_KEY")
        .env_remove("GOOGLE_API_KEY")
        .env_remove("ANTHROPIC_API_KEY")
        .env_remove("OPENAI_API_KEY")
        .env_remove("OPENROUTER_API_KEY")
        .env_remove("OLLAMA_API_KEY")
        .env_remove("OLLAMA_HOST")
        .env_remove("BLYTE_MODEL")
        .output()
        .unwrap()
}

#[test]
fn test_clean_verdict_accepted_unmerged_kept() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T1");
    git(
        root,
        &["worktree", "add", "-b", "wip/T1", wt_path.to_str().unwrap()],
    );

    write_file(&wt_path.join("file.txt"), "some work\n");
    git(&wt_path, &["add", "."]);
    git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T1.start.json"),
        &serde_json::json!({ "id": "T1", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("T1.json"),
        &serde_json::json!({ "outcome": "verified" }).to_string(),
    );
    write_file(
        &wt_dir.join("T1.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );
    write_file(&wt_dir.join("T1.log"), "log content\n");
    write_file(
        &wt_dir.join("T1.report.json"),
        &serde_json::json!({ "turns": 10 }).to_string(),
    );

    // Also place an unrelated directory in .wt to verify it is left alone
    write_file(&wt_dir.join("other_dir/some_file"), "leave me\n");

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("kept .wt/T1 (not merged yet)"),
        "stdout was: {stdout}"
    );
    assert!(stdout.contains("freed 0 B"), "stdout was: {stdout}");
    assert!(
        !stdout.contains("other_dir"),
        "stdout should not mention other_dir: {stdout}"
    );

    // Worktree directory kept
    assert!(wt_path.exists());
    // Record files stay
    assert!(wt_dir.join("T1.start.json").is_file());
    assert!(wt_dir.join("T1.json").is_file());
    assert!(wt_dir.join("T1.verdict.json").is_file());
    assert!(wt_dir.join("T1.log").is_file());
    assert!(wt_dir.join("T1.report.json").is_file());
    // Unrelated directory stays
    assert!(wt_dir.join("other_dir/some_file").is_file());

    // Branch wip/T1 stays
    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T1"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(branch_check.status.success());
}

#[test]
fn test_clean_cherry_picked_onto_main_deletes_branch() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T2");
    git(
        root,
        &["worktree", "add", "-b", "wip/T2", wt_path.to_str().unwrap()],
    );

    write_file(&wt_path.join("cherry.txt"), "cherry content\n");
    git(&wt_path, &["add", "."]);
    git(
        &wt_path,
        &["commit", "-qm", "feat: cherry", "--no-gpg-sign"],
    );
    let commit_sha = git(&wt_path, &["rev-parse", "HEAD"]);

    // Cherry pick onto main
    git(root, &["cherry-pick", &commit_sha]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T2.start.json"),
        &serde_json::json!({ "id": "T2", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("T2.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("removed .wt/T2 (merged,"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("deleted branch wip/T2 (merged)"),
        "stdout was: {stdout}"
    );

    assert!(!wt_path.exists());
    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T2"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(!branch_check.status.success(), "branch should be deleted");
}

#[test]
fn test_clean_squash_merged_runs_removed_by_merge_tree_check() {
    let repo = init_repo();
    let root = repo.path();

    let s1_path = root.join(".wt/S1");
    git(
        root,
        &["worktree", "add", "-b", "wip/S1", s1_path.to_str().unwrap()],
    );
    write_file(&s1_path.join("s1.txt"), "feature 1\n");
    git(&s1_path, &["add", "."]);
    git(&s1_path, &["commit", "-qm", "feat: s1", "--no-gpg-sign"]);

    let s2_path = root.join(".wt/S2");
    git(
        root,
        &["worktree", "add", "-b", "wip/S2", s2_path.to_str().unwrap()],
    );
    write_file(&s2_path.join("s2.txt"), "feature 2\n");
    git(&s2_path, &["add", "."]);
    git(&s2_path, &["commit", "-qm", "feat: s2", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("S1.start.json"),
        &serde_json::json!({ "id": "S1", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("S1.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );
    write_file(
        &wt_dir.join("S2.start.json"),
        &serde_json::json!({ "id": "S2", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("S2.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    // Create the squash commit directly on main combining both changes
    write_file(&root.join("s1.txt"), "feature 1\n");
    write_file(&root.join("s2.txt"), "feature 2\n");
    git(root, &["add", "."]);
    git(
        root,
        &["commit", "-qm", "squash: s1 and s2", "--no-gpg-sign"],
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("removed .wt/S1 (merged,"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("deleted branch wip/S1 (merged)"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("removed .wt/S2 (merged,"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("deleted branch wip/S2 (merged)"),
        "stdout was: {stdout}"
    );

    assert!(!s1_path.exists());
    assert!(!s2_path.exists());

    let b1 = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/S1"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(!b1.status.success());
    let b2 = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/S2"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(!b2.status.success());
}

#[test]
fn test_clean_local_non_wip_branch_not_merged_kept() {
    let repo = init_repo();
    let root = repo.path();

    let f1_path = root.join(".wt/F1");
    git(
        root,
        &["worktree", "add", "-b", "wip/F1", f1_path.to_str().unwrap()],
    );

    write_file(&f1_path.join("feat.txt"), "feature\n");
    git(&f1_path, &["add", "."]);
    git(&f1_path, &["commit", "-qm", "feat: f1", "--no-gpg-sign"]);

    // Create a local non-wip branch containing wip/F1's tip, but main does not have it
    git(root, &["branch", "feature/my-feat", "wip/F1"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("F1.start.json"),
        &serde_json::json!({ "id": "F1", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("F1.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("kept .wt/F1 (not merged yet)"),
        "stdout was: {stdout}"
    );
    assert!(f1_path.exists(), "worktree should be kept");

    let b1 = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/F1"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(b1.status.success(), "wip branch should be kept");
    let b2 = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/feature/my-feat"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(b2.status.success(), "feature branch should be kept");
}

#[test]
fn test_clean_verdict_redone_removes_worktree_keeps_unmerged_branch() {
    let repo = init_repo();
    let root = repo.path();

    let r1_path = root.join(".wt/R1");
    git(
        root,
        &["worktree", "add", "-b", "wip/R1", r1_path.to_str().unwrap()],
    );

    write_file(&r1_path.join("redone.txt"), "redone\n");
    git(&r1_path, &["add", "."]);
    git(&r1_path, &["commit", "-qm", "feat: r1", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("R1.start.json"),
        &serde_json::json!({ "id": "R1", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("R1.json"),
        &serde_json::json!({ "outcome": "verified" }).to_string(),
    );
    write_file(
        &wt_dir.join("R1.verdict.json"),
        &serde_json::json!({ "verdict": "redone" }).to_string(),
    );
    write_file(&wt_dir.join("R1.log"), "log content\n");
    write_file(
        &wt_dir.join("R1.report.json"),
        &serde_json::json!({ "turns": 5 }).to_string(),
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("removed .wt/R1 (verdict redone,"),
        "stdout was: {stdout}"
    );

    assert!(!r1_path.exists(), "worktree should be removed");
    assert!(wt_dir.join("R1.start.json").is_file());
    assert!(wt_dir.join("R1.json").is_file());
    assert!(wt_dir.join("R1.verdict.json").is_file());
    assert!(wt_dir.join("R1.log").is_file());
    assert!(wt_dir.join("R1.report.json").is_file());

    // Unmerged branch is kept
    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/R1"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(
        branch_check.status.success(),
        "unmerged branch wip/R1 must be kept"
    );
}

#[test]
fn test_clean_uncommitted_changes_kept() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T3");
    git(
        root,
        &["worktree", "add", "-b", "wip/T3", wt_path.to_str().unwrap()],
    );

    // Leave an untracked uncommitted file in the worktree
    write_file(&wt_path.join("uncommitted.txt"), "not committed\n");

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T3.start.json"),
        &serde_json::json!({ "id": "T3", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("T3.verdict.json"),
        &serde_json::json!({ "verdict": "redone" }).to_string(),
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("kept .wt/T3 (uncommitted changes)"),
        "stdout was: {stdout}"
    );

    assert!(wt_path.exists(), "worktree should be kept");
    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T3"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(branch_check.status.success(), "branch should be kept");
}

#[test]
fn test_clean_no_verdict_recent_kept_older_than_zero_removed() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T4");
    git(
        root,
        &["worktree", "add", "-b", "wip/T4", wt_path.to_str().unwrap()],
    );

    write_file(&wt_path.join("file.txt"), "work\n");
    git(&wt_path, &["add", "."]);
    git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T4.start.json"),
        &serde_json::json!({ "id": "T4", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("T4.json"),
        &serde_json::json!({ "outcome": "verified" }).to_string(),
    );

    // 1. blyte clean with default older-than (14 days) should keep it because it finished recently
    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success());
    assert!(
        stdout.contains("kept .wt/T4 (no verdict)"),
        "stdout was: {stdout}"
    );
    assert!(wt_path.exists(), "worktree should still exist");

    // 2. blyte clean --older-than 0 should remove it but keep its branch
    let out = blyte_output(root, &["clean", "--older-than", "0"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success());
    assert!(
        stdout.contains("removed .wt/T4 (finished over 0 days ago,"),
        "stdout was: {stdout}"
    );
    assert!(!wt_path.exists(), "worktree should now be removed");

    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T4"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(
        branch_check.status.success(),
        "unmerged branch wip/T4 must be kept"
    );
}

#[test]
fn test_clean_dry_run_changes_nothing() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T5");
    git(
        root,
        &["worktree", "add", "-b", "wip/T5", wt_path.to_str().unwrap()],
    );

    write_file(&wt_path.join("file.txt"), "work\n");
    git(&wt_path, &["add", "."]);
    git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T5.start.json"),
        &serde_json::json!({ "id": "T5", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("T5.verdict.json"),
        &serde_json::json!({ "verdict": "redone" }).to_string(),
    );

    let out = blyte_output(root, &["clean", "--dry-run"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success());
    assert!(
        stdout.contains("would remove .wt/T5 (verdict redone,"),
        "stdout was: {stdout}"
    );
    assert!(stdout.contains("would free "), "stdout was: {stdout}");

    assert!(wt_path.exists(), "worktree must not be removed in dry-run");
    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T5"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(branch_check.status.success(), "branch must still exist");
}

#[test]
fn test_review_verdict_accepted_unmerged_worktree_kept() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T6");
    git(
        root,
        &["worktree", "add", "-b", "wip/T6", wt_path.to_str().unwrap()],
    );

    write_file(&wt_path.join("file.txt"), "work\n");
    git(&wt_path, &["add", "."]);
    git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T6.start.json"),
        &serde_json::json!({ "id": "T6", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("T6.json"),
        &serde_json::json!({ "outcome": "verified" }).to_string(),
    );

    let out = blyte_output(root, &["review", "T6", "--verdict", "accepted"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "review failed: {stdout}");
    assert!(
        stdout.contains("recorded verdict accepted for T6"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("worktree .wt/T6 kept until wip/T6 is merged"),
        "stdout was: {stdout}"
    );
    assert!(!stdout.contains("removed"), "stdout was: {stdout}");

    assert!(wt_path.exists(), "worktree should be kept");
    assert!(
        wt_dir.join("T6.verdict.json").is_file(),
        "verdict file should exist"
    );

    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T6"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(branch_check.status.success(), "branch should be kept");
}

#[test]
fn test_review_verdict_redone_removes_worktree_keeps_branch() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T7");
    git(
        root,
        &["worktree", "add", "-b", "wip/T7", wt_path.to_str().unwrap()],
    );

    write_file(&wt_path.join("file.txt"), "work\n");
    git(&wt_path, &["add", "."]);
    git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T7.start.json"),
        &serde_json::json!({ "id": "T7", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("T7.json"),
        &serde_json::json!({ "outcome": "verified" }).to_string(),
    );

    let out = blyte_output(root, &["review", "T7", "--verdict", "redone"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "review failed: {stdout}");
    assert!(
        stdout.contains("recorded verdict redone for T7"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("removed .wt/T7; branch wip/T7 kept"),
        "stdout was: {stdout}"
    );

    assert!(!wt_path.exists(), "worktree should be removed");
    assert!(
        wt_dir.join("T7.verdict.json").is_file(),
        "verdict file should exist"
    );

    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T7"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(branch_check.status.success(), "branch should be kept");
}

#[test]
fn test_review_verdict_accepted_merged_removes_worktree_deletes_branch() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T8");
    git(
        root,
        &["worktree", "add", "-b", "wip/T8", wt_path.to_str().unwrap()],
    );

    write_file(&wt_path.join("merged.txt"), "merged content\n");
    git(&wt_path, &["add", "."]);
    git(
        &wt_path,
        &["commit", "-qm", "feat: merged", "--no-gpg-sign"],
    );
    let commit_sha = git(&wt_path, &["rev-parse", "HEAD"]);

    // Cherry pick onto main so work is merged
    git(root, &["cherry-pick", &commit_sha]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T8.start.json"),
        &serde_json::json!({ "id": "T8", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("T8.json"),
        &serde_json::json!({ "outcome": "verified" }).to_string(),
    );

    let out = blyte_output(root, &["review", "T8", "--verdict", "accepted"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "review failed: {stdout}");
    assert!(
        stdout.contains("recorded verdict accepted for T8"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("removed .wt/T8; branch wip/T8 deleted"),
        "stdout was: {stdout}"
    );

    assert!(!wt_path.exists(), "worktree should be removed");
    let branch_check = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T8"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(!branch_check.status.success(), "branch should be deleted");
}

#[test]
fn test_clean_outcome_no_changes() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/NC1");
    git(
        root,
        &[
            "worktree",
            "add",
            "-b",
            "wip/NC1",
            wt_path.to_str().unwrap(),
        ],
    );

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("NC1.start.json"),
        &serde_json::json!({ "id": "NC1", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("NC1.json"),
        &serde_json::json!({ "outcome": "no_changes" }).to_string(),
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success());
    assert!(
        stdout.contains("removed .wt/NC1 (no changes,"),
        "stdout was: {stdout}"
    );
    assert!(!wt_path.exists(), "worktree should be removed");
}

#[test]
fn test_clean_research_run() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/R1");
    std::fs::create_dir_all(&wt_path).unwrap();
    write_file(&wt_path.join("scratch.txt"), "some notes\n");

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("R1.start.json"),
        &serde_json::json!({ "id": "R1", "pid": 999999, "research": true }).to_string(),
    );
    write_file(&wt_dir.join("R1.report.md"), "# Research Report\n");

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success());
    assert!(
        stdout.contains("removed .wt/R1 (research report,"),
        "stdout was: {stdout}"
    );
    assert!(!wt_path.exists(), "scratch directory should be removed");
}

#[test]
fn test_dispatch_cleans_quietly() {
    let repo = init_repo();
    let root = repo.path();

    // Create a finished worktree with a verdict that should be cleaned up by dispatch
    let wt_old = root.join(".wt/OLD");
    git(
        root,
        &["worktree", "add", "-b", "wip/OLD", wt_old.to_str().unwrap()],
    );

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("OLD.start.json"),
        &serde_json::json!({ "id": "OLD", "pid": 999999 }).to_string(),
    );
    write_file(
        &wt_dir.join("OLD.verdict.json"),
        &serde_json::json!({ "verdict": "redone" }).to_string(),
    );

    write_file(&root.join("task.md"), "implement something\n");

    // Run dispatch for D1 with a non-existent or failing gate, but verify OLD was cleaned
    let out = blyte_output(
        root,
        &[
            "dispatch",
            "D1",
            "task.md",
            "--gate",
            "true",
            "--sandbox",
            "off",
            "--no-base-check",
        ],
    );

    let _ = out;
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
    while wt_old.exists() && std::time::Instant::now() < deadline {
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
    assert!(!wt_old.exists(), "old worktree should have been removed");
}

/// A local main far behind origin/main (1,292 commits in one repository, 2026-10-01): work merged
/// on the remote counts as merged as of the last fetch.
#[test]
fn test_clean_merged_on_origin_main_only() {
    let repo = init_repo();
    let root = repo.path();

    let wt_path = root.join(".wt/T1");
    git(
        root,
        &["worktree", "add", "-b", "wip/T1", wt_path.to_str().unwrap()],
    );
    write_file(&wt_path.join("file.txt"), "some work\n");
    git(&wt_path, &["add", "."]);
    git(&wt_path, &["commit", "-qm", "feat: work", "--no-gpg-sign"]);
    let tip = git(&wt_path, &["rev-parse", "HEAD"]);

    // origin/main has the work; local main does not.
    git(root, &["update-ref", "refs/remotes/origin/main", &tip]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("T1.start.json"),
        &serde_json::json!({ "id": "T1", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("T1.json"),
        &serde_json::json!({ "outcome": "verified" }).to_string(),
    );
    write_file(
        &wt_dir.join("T1.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    let out = blyte_output(root, &["clean"]);
    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("removed .wt/T1 (merged"),
        "stdout was: {stdout}"
    );
    assert!(!wt_path.exists());
}

#[test]
fn test_clean_squash_merged_run_lines_later_changed_on_main() {
    let repo = init_repo();
    let root = repo.path();

    write_file(&root.join(".gitignore"), ".wt/\n");
    git(root, &["add", ".gitignore"]);
    git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);

    let s1_path = root.join(".wt/S1");
    git(
        root,
        &["worktree", "add", "-b", "wip/S1", s1_path.to_str().unwrap()],
    );
    write_file(
        &s1_path.join("code.rs"),
        "fn hello() {\n    println!(\"hello from S1\");\n}\n",
    );
    git(&s1_path, &["add", "."]);
    git(&s1_path, &["commit", "-qm", "feat: s1", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("S1.start.json"),
        &serde_json::json!({ "id": "S1", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("S1.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    // Squash-merge S1 into main
    git(root, &["checkout", "main"]);
    git(root, &["merge", "--squash", "wip/S1"]);
    git(root, &["commit", "-qm", "squash merge S1", "--no-gpg-sign"]);

    // Main changes the same lines again!
    write_file(
        &root.join("code.rs"),
        "fn hello() {\n    println!(\"hello from main later\");\n}\n",
    );
    git(root, &["add", "."]);
    git(
        root,
        &["commit", "-qm", "main: edit code again", "--no-gpg-sign"],
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("removed .wt/S1 (merged,"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("deleted branch wip/S1 (merged)"),
        "stdout was: {stdout}"
    );
    assert!(!s1_path.exists(), "worktree should be removed");
    let b = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/S1"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(!b.status.success(), "wip/S1 branch should be deleted");
}

#[test]
fn test_clean_unmerged_run_whose_files_main_later_changed_is_kept() {
    let repo = init_repo();
    let root = repo.path();

    write_file(&root.join(".gitignore"), ".wt/\n");
    git(root, &["add", ".gitignore"]);
    git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);

    write_file(
        &root.join("code.rs"),
        "fn hello() {\n    println!(\"hello\");\n}\n",
    );
    git(root, &["add", "."]);
    git(root, &["commit", "-qm", "init code", "--no-gpg-sign"]);

    let u1_path = root.join(".wt/U1");
    git(
        root,
        &["worktree", "add", "-b", "wip/U1", u1_path.to_str().unwrap()],
    );
    write_file(
        &u1_path.join("code.rs"),
        "fn hello() {\n    println!(\"unmerged work\");\n}\n",
    );
    git(&u1_path, &["add", "."]);
    git(&u1_path, &["commit", "-qm", "feat: u1", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    write_file(
        &wt_dir.join("U1.start.json"),
        &serde_json::json!({ "id": "U1", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("U1.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    // Main changes the file, but U1 was never merged
    write_file(
        &root.join("code.rs"),
        "fn hello() {\n    println!(\"completely different on main\");\n}\n",
    );
    git(root, &["add", "."]);
    git(
        root,
        &["commit", "-qm", "main: change code", "--no-gpg-sign"],
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("kept .wt/U1 (not merged yet)"),
        "stdout was: {stdout}"
    );
    assert!(u1_path.exists(), "worktree should be kept");
    let b = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/U1"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(b.status.success(), "wip/U1 branch should be kept");
}

#[test]
fn test_clean_cherry_pick_record_squash_commit_blyte_clean_and_quiet() {
    let repo = init_repo();
    let root = repo.path();

    write_file(&root.join(".gitignore"), ".wt/\n");
    git(root, &["add", ".gitignore"]);
    git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);

    let content = "line 1: initial\nline 2: initial\nline 3: initial\n";
    write_file(&root.join("file.txt"), content);
    git(root, &["add", "file.txt"]);
    git(root, &["commit", "-qm", "initial commit", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    std::fs::create_dir_all(&wt_dir).unwrap();

    // T1 modifies line 1
    let t1_path = wt_dir.join("T1");
    git(
        root,
        &["worktree", "add", "-b", "wip/T1", t1_path.to_str().unwrap()],
    );
    let t1_content = "line 1: T1 changed\nline 2: initial\nline 3: initial\n";
    write_file(&t1_path.join("file.txt"), t1_content);
    git(&t1_path, &["commit", "-am", "T1 work", "--no-gpg-sign"]);
    let t1_sha = git(&t1_path, &["rev-parse", "HEAD"]);

    write_file(
        &wt_dir.join("T1.start.json"),
        &serde_json::json!({ "id": "T1", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("T1.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    // T1b modifies line 2
    let t1b_path = wt_dir.join("T1b");
    git(
        root,
        &[
            "worktree",
            "add",
            "-b",
            "wip/T1b",
            t1b_path.to_str().unwrap(),
        ],
    );
    let t1b_content = "line 1: initial\nline 2: T1b changed\nline 3: initial\n";
    write_file(&t1b_path.join("file.txt"), t1b_content);
    git(&t1b_path, &["commit", "-am", "T1b work", "--no-gpg-sign"]);
    let t1b_sha = git(&t1b_path, &["rev-parse", "HEAD"]);

    write_file(
        &wt_dir.join("T1b.start.json"),
        &serde_json::json!({ "id": "T1b", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("T1b.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    // Main changes line 1 and line 2 in the squash commit (so merge-tree conflicts with both!)
    let main_content =
        "line 1: main squash conflict\nline 2: main squash conflict\nline 3: initial\n";
    write_file(&root.join("file.txt"), main_content);
    let commit_msg = format!(
        "Squash merge PR\n\n* feat: T1\n  (cherry picked from commit {t1_sha})\n* feat: T1b\n  (cherry picked from commit {t1b_sha})\n"
    );
    git(root, &["commit", "-am", &commit_msg, "--no-gpg-sign"]);

    // Verify merge-tree conflicts with main for both T1 and T1b
    let mt1 = Command::new("git")
        .args(["merge-tree", "--write-tree", "main", "wip/T1"])
        .current_dir(root)
        .output()
        .unwrap();
    let main_tree = git(root, &["rev-parse", "main^{tree}"]);
    assert!(
        !mt1.status.success()
            || String::from_utf8_lossy(&mt1.stdout)
                .lines()
                .next()
                .unwrap_or("")
                != main_tree
    );

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("removed .wt/T1 (merged,"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("deleted branch wip/T1 (merged)"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("removed .wt/T1b (merged,"),
        "stdout was: {stdout}"
    );
    assert!(
        stdout.contains("deleted branch wip/T1b (merged)"),
        "stdout was: {stdout}"
    );
    assert!(!t1_path.exists());
    assert!(!t1b_path.exists());
}

#[test]
fn test_clean_cherry_pick_record_squash_commit_quiet_clean() {
    let repo = init_repo();
    let root = repo.path();

    write_file(&root.join(".gitignore"), ".wt/\n");
    git(root, &["add", ".gitignore"]);
    git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);

    let content = "line 1: initial\nline 2: initial\nline 3: initial\n";
    write_file(&root.join("file.txt"), content);
    git(root, &["add", "file.txt"]);
    git(root, &["commit", "-qm", "initial commit", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    std::fs::create_dir_all(&wt_dir).unwrap();

    // T1 modifies line 1
    let t1_path = wt_dir.join("T1");
    git(
        root,
        &["worktree", "add", "-b", "wip/T1", t1_path.to_str().unwrap()],
    );
    let t1_content = "line 1: T1 changed\nline 2: initial\nline 3: initial\n";
    write_file(&t1_path.join("file.txt"), t1_content);
    git(&t1_path, &["commit", "-am", "T1 work", "--no-gpg-sign"]);
    let t1_sha = git(&t1_path, &["rev-parse", "HEAD"]);

    write_file(
        &wt_dir.join("T1.start.json"),
        &serde_json::json!({ "id": "T1", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("T1.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    // T1b modifies line 2
    let t1b_path = wt_dir.join("T1b");
    git(
        root,
        &[
            "worktree",
            "add",
            "-b",
            "wip/T1b",
            t1b_path.to_str().unwrap(),
        ],
    );
    let t1b_content = "line 1: initial\nline 2: T1b changed\nline 3: initial\n";
    write_file(&t1b_path.join("file.txt"), t1b_content);
    git(&t1b_path, &["commit", "-am", "T1b work", "--no-gpg-sign"]);
    let t1b_sha = git(&t1b_path, &["rev-parse", "HEAD"]);

    write_file(
        &wt_dir.join("T1b.start.json"),
        &serde_json::json!({ "id": "T1b", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("T1b.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    // Main changes line 1 and line 2 in the squash commit (so merge-tree conflicts with both!)
    let main_content =
        "line 1: main squash conflict\nline 2: main squash conflict\nline 3: initial\n";
    write_file(&root.join("file.txt"), main_content);
    let commit_msg = format!(
        "Squash merge PR\n\n* feat: T1\n  (cherry picked from commit {t1_sha})\n* feat: T1b\n  (cherry picked from commit {t1b_sha})\n"
    );
    git(root, &["commit", "-am", &commit_msg, "--no-gpg-sign"]);

    blyte_cli::clean::clean_repo(root, 14, false, None, true);

    assert!(!t1_path.exists());
    assert!(!t1b_path.exists());
    let b1 = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T1"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(!b1.status.success(), "wip/T1 branch should be deleted");
    let b2 = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T1b"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(!b2.status.success(), "wip/T1b branch should be deleted");
}

#[test]
fn test_clean_cherry_pick_record_partial_commits_not_merged() {
    let repo = init_repo();
    let root = repo.path();

    write_file(&root.join(".gitignore"), ".wt/\n");
    git(root, &["add", ".gitignore"]);
    git(root, &["commit", "-qm", "ignore .wt", "--no-gpg-sign"]);

    let wt_dir = root.join(".wt");
    std::fs::create_dir_all(&wt_dir).unwrap();

    let t2_path = wt_dir.join("T2");
    git(
        root,
        &["worktree", "add", "-b", "wip/T2", t2_path.to_str().unwrap()],
    );

    // Commit 1 on wip/T2
    write_file(&t2_path.join("file1.txt"), "commit 1\n");
    git(&t2_path, &["add", "file1.txt"]);
    git(
        &t2_path,
        &["commit", "-qm", "feat: commit 1", "--no-gpg-sign"],
    );
    let c1_sha = git(&t2_path, &["rev-parse", "HEAD"]);

    // Commit 2 on wip/T2
    write_file(&t2_path.join("file2.txt"), "commit 2\n");
    git(&t2_path, &["add", "file2.txt"]);
    git(
        &t2_path,
        &["commit", "-qm", "feat: commit 2", "--no-gpg-sign"],
    );

    write_file(
        &wt_dir.join("T2.start.json"),
        &serde_json::json!({ "id": "T2", "pid": 999999, "started_at": 1000 }).to_string(),
    );
    write_file(
        &wt_dir.join("T2.verdict.json"),
        &serde_json::json!({ "verdict": "accepted" }).to_string(),
    );

    // Main has only commit 1 in the cherry-pick record, NOT commit 2
    let commit_msg =
        format!("Squash PR with only first commit\n\n(cherry picked from commit {c1_sha})\n");
    write_file(&root.join("other.txt"), "other work\n");
    git(root, &["add", "other.txt"]);
    git(root, &["commit", "-qm", &commit_msg, "--no-gpg-sign"]);

    let out = blyte_output(root, &["clean"]);

    let stdout = String::from_utf8_lossy(&out.stdout);
    assert!(out.status.success(), "clean failed: {stdout}");
    assert!(
        stdout.contains("kept .wt/T2 (not merged yet)"),
        "stdout was: {stdout}"
    );
    assert!(t2_path.exists(), "worktree should be kept");
    let b = Command::new("git")
        .args(["rev-parse", "--verify", "refs/heads/wip/T2"])
        .current_dir(root)
        .output()
        .unwrap();
    assert!(b.status.success(), "wip/T2 branch should be kept");
}