orch-cli 0.105.7

Orch, the agent orchestration engine: runs coding agents on tasks and cron jobs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
use super::rebase::apply_rebase_recovery_outcome;
use super::rebase::classify_fetch_failure;
use super::rebase::is_ssh_agent_fetch_failure;
use super::*;
use crate::backends::test_helpers::NoopBackend;
use crate::backends::{ExternalId, ExternalTask};
use crate::engine::ci::any_check_pending;
use crate::engine::ci::ci_failure_sha_key;
use crate::engine::ci::ci_pending_since_key;
use crate::engine::ci::ci_pending_since_prefix;
use crate::engine::ci::sweep_ci_timer_keys;
use crate::engine::git::git_ops;
use crate::engine::router;
use crate::engine::tasks::TaskManager;
use crate::github::token;
use crate::github::types::GitHubApp;
use crate::github::types::GitHubBranchRef;
use crate::github::types::GitHubPullRequest;
use crate::github::types::RequiredStatusCheck;
use crate::github::types::{GitHubReview, GitHubUser};
use crate::store::NewTask;
use crate::store::TaskStatus;
use crate::store::TaskStore;
use std::env;
use wiremock::matchers::{method, path, query_param};
use wiremock::{Mock, MockServer, ResponseTemplate};

#[test]
fn mergeable_state_ready_accepts_only_clean_states() {
    assert!(mergeable_state_ready("clean", &[], &[], &[]));
    assert!(mergeable_state_ready("has_hooks", &[], &[], &[]));
    for state in ["behind", "blocked", "unknown", "dirty", "draft"] {
        assert!(
            !mergeable_state_ready(state, &[], &[], &[]),
            "{state} must hold the merge"
        );
    }
}

#[test]
fn mergeable_state_ready_accepts_unstable_when_required_checks_passed() {
    let required = vec![required_check("ci", None)];
    let check_runs = vec![
        check_run("ci", "completed", Some("success"), None),
        check_run("lint", "completed", Some("failure"), None),
    ];
    assert!(mergeable_state_ready(
        "unstable",
        &required,
        &check_runs,
        &[]
    ));
    assert_eq!(
        failing_optional_check_names(&required, &check_runs, &[]),
        vec!["lint"]
    );
}

#[test]
fn mergeable_state_ready_holds_unstable_when_a_required_check_failed() {
    let required = vec![required_check("ci", None)];
    let check_runs = vec![
        check_run("ci", "completed", Some("failure"), None),
        check_run("lint", "completed", Some("success"), None),
    ];
    assert!(!mergeable_state_ready(
        "unstable",
        &required,
        &check_runs,
        &[]
    ));
}

#[test]
fn mergeable_state_ready_holds_unstable_while_checks_are_pending_without_required() {
    let check_runs = vec![check_run("ci", "in_progress", None, None)];
    assert!(!mergeable_state_ready("unstable", &[], &check_runs, &[]));
    let statuses = vec![("lint".to_string(), "pending".to_string())];
    assert!(!mergeable_state_ready("unstable", &[], &[], &statuses));
}

#[test]
fn mergeable_state_ready_accepts_unstable_when_optional_checks_finished_without_required() {
    let check_runs = vec![check_run("lint", "completed", Some("failure"), None)];
    assert!(mergeable_state_ready("unstable", &[], &check_runs, &[]));
    assert_eq!(
        failing_optional_check_names(&[], &check_runs, &[]),
        vec!["lint"]
    );
    let action_required = vec![check_run(
        "deploy",
        "completed",
        Some("action_required"),
        None,
    )];
    assert!(!mergeable_state_ready(
        "unstable",
        &[],
        &action_required,
        &[]
    ));
    assert!(failing_optional_check_names(&[], &action_required, &[]).is_empty());
}

/// Auto-merge refuses a PR whose head does not contain the current base
/// commit: its green checks ran against older code (#3780)
#[test]
fn may_merge_onto_base_refuses_a_head_behind_its_base() {
    assert!(
        may_merge_onto_base(Some(0)),
        "a head that contains the base tip may merge"
    );
    assert!(
        !may_merge_onto_base(Some(1)),
        "a head one commit behind the base must not merge"
    );
    assert!(
        !may_merge_onto_base(Some(12)),
        "a head well behind the base must not merge"
    );
    assert!(
        !may_merge_onto_base(None),
        "an unreadable comparison must hold the merge, not permit it"
    );
}

fn make_review(login: &str, state: &str, submitted_at: &str) -> GitHubReview {
    GitHubReview {
        id: 1,
        user: GitHubUser {
            login: login.to_string(),
        },
        body: None,
        state: state.to_string(),
        html_url: None,
        submitted_at: submitted_at.to_string(),
        commit_id: None,
    }
}

fn required_check(context: &str, integration_id: Option<i64>) -> RequiredStatusCheck {
    RequiredStatusCheck {
        context: context.to_string(),
        integration_id,
    }
}

fn check_run(
    name: &str,
    status: &str,
    conclusion: Option<&str>,
    app_id: Option<i64>,
) -> GitHubCheckRun {
    GitHubCheckRun {
        id: 1,
        name: name.to_string(),
        status: status.to_string(),
        conclusion: conclusion.map(str::to_string),
        app: app_id.map(|id| GitHubApp { id }),
    }
}

#[test]
fn any_check_pending_ignores_the_reviews_own_checks() {
    let runs = vec![
        check_run("ci", "completed", Some("success"), None),
        check_run("review-gate", "in_progress", None, None),
        check_run("orch/review", "queued", None, None),
    ];
    let statuses = vec![("orch/review".to_string(), "pending".to_string())];
    assert!(!any_check_pending(&runs, &statuses));

    let runs = vec![check_run("ci", "in_progress", None, None)];
    assert!(any_check_pending(&runs, &[]));
}

#[test]
fn required_checks_state_uses_required_contexts() {
    let required = vec![required_check("ci", None), required_check("lint", None)];
    let check_runs = vec![
        check_run("ci", "completed", Some("success"), None),
        check_run("lint", "completed", Some("failure"), None),
    ];
    let statuses = vec![("unused".to_string(), "success".to_string())];

    let (state, total, passing, failing, pending) =
        required_checks_state(&required, &check_runs, &statuses);

    assert_eq!(state, "failure");
    assert_eq!(total, 2);
    assert_eq!(passing, 1);
    assert_eq!(failing, 1);
    assert_eq!(pending, 0);
}

#[test]
fn required_checks_state_ruleset_missing_run_is_pending() {
    let required = vec![required_check("ci/test", Some(42))];
    let (state, total, passing, failing, pending) = required_checks_state(&required, &[], &[]);

    assert_eq!(state, "pending");
    assert_eq!(total, 1);
    assert_eq!(passing, 0);
    assert_eq!(failing, 0);
    assert_eq!(pending, 1);
}

#[test]
fn required_checks_state_ruleset_passing_run_is_success() {
    let required = vec![required_check("ci/test", Some(42))];
    let check_runs = vec![check_run("ci/test", "completed", Some("success"), Some(42))];
    let (state, total, passing, failing, pending) =
        required_checks_state(&required, &check_runs, &[]);

    assert_eq!(state, "success");
    assert_eq!(total, 1);
    assert_eq!(passing, 1);
    assert_eq!(failing, 0);
    assert_eq!(pending, 0);
}

#[test]
fn required_checks_state_ruleset_failing_run_is_failure() {
    let required = vec![required_check("ci/test", Some(42))];
    let check_runs = vec![check_run("ci/test", "completed", Some("failure"), Some(42))];
    let (state, ..) = required_checks_state(&required, &check_runs, &[]);
    assert_eq!(state, "failure");
}

#[test]
fn required_checks_state_action_required_is_pending() {
    let required = vec![required_check("ci/test", Some(42))];
    let check_runs = vec![check_run(
        "ci/test",
        "completed",
        Some("action_required"),
        Some(42),
    )];
    let (state, total, passing, failing, pending) =
        required_checks_state(&required, &check_runs, &[]);
    assert_eq!(state, "pending");
    assert_eq!(total, 1);
    assert_eq!(passing, 0);
    assert_eq!(failing, 0);
    assert_eq!(pending, 1);
}

#[test]
fn required_checks_state_wrong_app_does_not_satisfy() {
    let required = vec![required_check("ci/test", Some(42))];
    let check_runs = vec![check_run("ci/test", "completed", Some("success"), Some(99))];
    let (state, total, passing, failing, pending) =
        required_checks_state(&required, &check_runs, &[]);

    assert_eq!(state, "pending");
    assert_eq!(total, 1);
    assert_eq!(passing, 0);
    assert_eq!(failing, 0);
    assert_eq!(pending, 1);
}

#[test]
fn required_checks_state_classic_name_only_still_matches() {
    let required = vec![required_check("ci/test", None)];
    let check_runs = vec![check_run("ci/test", "completed", Some("success"), Some(99))];
    let (state, ..) = required_checks_state(&required, &check_runs, &[]);
    assert_eq!(state, "success");
}

#[test]
fn required_checks_state_empty_required_is_success() {
    let (state, total, passing, failing, pending) = required_checks_state(&[], &[], &[]);
    assert_eq!(state, "success");
    assert_eq!(total, 0);
    assert_eq!(passing, 0);
    assert_eq!(failing, 0);
    assert_eq!(pending, 0);
}

#[test]
fn required_checks_state_commit_status_cannot_satisfy_app_bound_rule() {
    let required = vec![required_check("ci/test", Some(42))];
    let statuses = vec![("ci/test".to_string(), "success".to_string())];
    let (state, _total, _passing, _failing, pending) =
        required_checks_state(&required, &[], &statuses);
    assert_eq!(state, "pending");
    assert_eq!(pending, 1);
}

#[test]
fn required_checks_state_commit_status_satisfies_name_only_rule() {
    let required = vec![required_check("ci/legacy", None)];
    let statuses = vec![("ci/legacy".to_string(), "success".to_string())];
    let (state, total, passing, failing, pending) =
        required_checks_state(&required, &[], &statuses);
    assert_eq!(state, "success");
    assert_eq!(total, 1);
    assert_eq!(passing, 1);
    assert_eq!(failing, 0);
    assert_eq!(pending, 0);
}

#[test]
fn required_checks_state_commit_status_failure_and_error_fail() {
    let required = vec![required_check("ci/legacy", None)];
    for state_name in ["failure", "error"] {
        let statuses = vec![("ci/legacy".to_string(), state_name.to_string())];
        let (state, ..) = required_checks_state(&required, &[], &statuses);
        assert_eq!(state, "failure", "{state_name}");
    }
}

#[test]
fn required_checks_state_commit_status_pending_is_pending() {
    let required = vec![required_check("ci/legacy", None)];
    let statuses = vec![("ci/legacy".to_string(), "pending".to_string())];
    let (state, total, passing, failing, pending) =
        required_checks_state(&required, &[], &statuses);
    assert_eq!(state, "pending");
    assert_eq!(total, 1);
    assert_eq!(passing, 0);
    assert_eq!(failing, 0);
    assert_eq!(pending, 1);
}

#[test]
fn required_checks_state_commit_status_neutral_and_skipped_pass() {
    let required = vec![required_check("ci/legacy", None)];
    for state_name in ["neutral", "skipped"] {
        let statuses = vec![("ci/legacy".to_string(), state_name.to_string())];
        let (state, total, passing, failing, pending) =
            required_checks_state(&required, &[], &statuses);
        assert_eq!(state, "success", "{state_name}");
        assert_eq!((total, passing, failing, pending), (1, 1, 0, 0));
    }
}

#[test]
fn failing_optional_check_names_skips_a_required_commit_status() {
    let required = vec![required_check("ci", None)];
    let statuses = vec![
        ("ci".to_string(), "failure".to_string()),
        ("lint".to_string(), "error".to_string()),
    ];
    assert_eq!(
        failing_optional_check_names(&required, &[], &statuses),
        vec!["lint"]
    );
}

#[test]
fn failing_optional_check_names_keeps_a_commit_status_when_the_rule_is_app_bound() {
    let required = vec![required_check("ci", Some(42))];
    let statuses = vec![("ci".to_string(), "failure".to_string())];
    assert_eq!(
        failing_optional_check_names(&required, &[], &statuses),
        vec!["ci"]
    );
}

#[test]
fn required_checks_state_check_run_without_app_does_not_satisfy_app_bound_rule() {
    let required = vec![required_check("ci/test", Some(42))];
    let check_runs = vec![check_run("ci/test", "completed", Some("success"), None)];
    let (state, ..) = required_checks_state(&required, &check_runs, &[]);
    assert_eq!(state, "pending");
}

#[test]
fn test_any_changes_requested_returns_true_when_latest_is_changes_requested() {
    let reviews = vec![
        make_review("alice", "APPROVED", "2026-01-01T10:00:00Z"),
        make_review("alice", "CHANGES_REQUESTED", "2026-01-01T11:00:00Z"),
    ];
    assert!(
        any_changes_requested_in_reviews(&reviews),
        "should return true when reviewer's latest review is CHANGES_REQUESTED"
    );
}

#[test]
fn test_any_changes_requested_returns_false_when_latest_is_approved() {
    let reviews = vec![
        make_review("alice", "CHANGES_REQUESTED", "2026-01-01T10:00:00Z"),
        make_review("alice", "APPROVED", "2026-01-01T11:00:00Z"),
    ];
    assert!(
        !any_changes_requested_in_reviews(&reviews),
        "should return false when reviewer's latest review is APPROVED"
    );
}

#[test]
fn test_any_changes_requested_returns_false_for_empty_reviews() {
    assert!(
        !any_changes_requested_in_reviews(&[]),
        "should return false for empty review list"
    );
}

#[test]
fn test_any_changes_requested_ignores_commented_state() {
    let reviews = vec![make_review("alice", "COMMENTED", "2026-01-01T10:00:00Z")];
    assert!(
        !any_changes_requested_in_reviews(&reviews),
        "COMMENTED state should be ignored"
    );
}

#[test]
fn test_any_changes_requested_one_reviewer_blocks_even_if_other_approved() {
    let reviews = vec![
        make_review("alice", "APPROVED", "2026-01-01T10:00:00Z"),
        make_review("bob", "CHANGES_REQUESTED", "2026-01-01T10:00:00Z"),
    ];
    assert!(
        any_changes_requested_in_reviews(&reviews),
        "should return true if any reviewer has outstanding CHANGES_REQUESTED"
    );
}

/// Regression test: handle_review_changes must set status to Routed (not New)
/// when a valid model is available for the task's agent via model_for_complexity().
/// This preserves the documented review-cycle architecture where re-dispatch
/// reuses existing routing context instead of forcing full LLM re-routing.
#[tokio::test]
async fn handle_review_changes_sets_routed_with_valid_model() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let repo = "owner/repo";

    let task_id_num = store
        .create(&NewTask {
            external_id: None,
            repo: repo.to_string(),
            origin: "internal".to_string(),
            title: "Implement feature X".to_string(),
            body: "body".to_string(),
            source: "cron".to_string(),
            source_id: "daily".to_string(),
            author: String::new(),
            url: String::new(),
            labels: vec![],
            parent_id: None,
        })
        .await
        .unwrap();
    store
        .update_status(task_id_num, TaskStatus::InReview)
        .await
        .unwrap();

    let task_id_str = format!("internal:{task_id_num}");
    let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);
    let task_manager = Arc::new(TaskManager::with_store(
        Arc::clone(&backend),
        Arc::clone(&store),
        repo.to_string(),
    ));

    let task = ExternalTask {
        id: ExternalId(task_id_str.clone()),
        title: "Implement feature X".to_string(),
        body: "body".to_string(),
        state: "open".to_string(),
        labels: vec![],
        author: String::new(),
        created_at: "2026-01-01T00:00:00Z".to_string(),
        updated_at: "2026-01-01T00:00:00Z".to_string(),
        url: String::new(),
    };

    let result = handle_review_changes(
        &task,
        "Please fix the type error on line 42",
        &[],
        &backend,
        repo,
        101,
        "claude",
        "sonnet",
        &task_manager,
        &store,
        &router::RouterConfig::default(),
    )
    .await;

    assert!(
        result.is_ok(),
        "handle_review_changes returned Err: {result:?}"
    );

    let updated = store.get(task_id_num).await.unwrap();
    // When the agent has no model pools configured (default test config),
    // model_for_complexity returns None → falls back to Status::New.
    assert_eq!(
        updated.status,
        TaskStatus::New,
        "handle_review_changes must fall back to New when model_for_complexity returns None"
    );

    assert!(
        !updated.pr_review_context.is_empty(),
        "pr_review_context must be stored so the re-dispatched agent sees the feedback"
    );
    assert!(
        !updated
            .pr_review_context
            .starts_with("A reviewer has requested changes"),
        "pr_review_context must NOT start with the template header"
    );
    assert!(
        updated
            .pr_review_context
            .contains("Please fix the type error on line 42"),
        "pr_review_context must contain the review notes passed to handle_review_changes"
    );

    assert_eq!(
        updated.review_cycles, 1,
        "review_cycles must be incremented on each review change request"
    );

    assert_eq!(
        updated.review_agent_failures, 0,
        "review_agent_failures must be reset to 0 when re-routing after review changes"
    );
}

/// Regression: handle_review_changes must resolve a valid model via
/// model_for_complexity() instead of reusing the review agent's model.
/// When the task's agent has a valid model available, it sets Routed.
/// When all models are cooled, it falls back to New for full re-route (#1723).
#[tokio::test]
async fn handle_review_changes_resolves_valid_model_or_falls_back() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let repo = "owner/repo";

    let task_id_num = store
        .create(&NewTask {
            external_id: None,
            repo: repo.to_string(),
            origin: "internal".to_string(),
            title: "Test".to_string(),
            body: "body".to_string(),
            source: "cron".to_string(),
            source_id: "daily".to_string(),
            author: String::new(),
            url: String::new(),
            labels: vec![],
            parent_id: None,
        })
        .await
        .unwrap();
    store
        .update_status(task_id_num, TaskStatus::InReview)
        .await
        .unwrap();

    // Pre-set agent/model/complexity to simulate a stored routing.
    // The model "opus" may not be valid for this agent's pool, but
    // model_for_complexity will resolve a valid one.
    store
        .set_fields(
            task_id_num,
            &[
                ("agent", serde_json::json!("claude")),
                ("model", serde_json::json!("opus")),
                ("complexity", serde_json::json!("medium")),
                (
                    "route_reason",
                    serde_json::json!("llm classified as medium"),
                ),
            ],
        )
        .await
        .unwrap();

    let task_id_str = format!("internal:{task_id_num}");
    let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);
    let task_manager = Arc::new(TaskManager::with_store(
        Arc::clone(&backend),
        Arc::clone(&store),
        repo.to_string(),
    ));

    let task = ExternalTask {
        id: ExternalId(task_id_str.clone()),
        title: "Test".to_string(),
        body: "body".to_string(),
        state: "open".to_string(),
        labels: vec![],
        author: String::new(),
        created_at: "2026-01-01T00:00:00Z".to_string(),
        updated_at: "2026-01-01T00:00:00Z".to_string(),
        url: String::new(),
    };

    let result = handle_review_changes(
        &task,
        "Fix the bug",
        &[],
        &backend,
        repo,
        42,
        "minimax",
        "opus",
        &task_manager,
        &store,
        &router::RouterConfig::default(),
    )
    .await;

    assert!(
        result.is_ok(),
        "handle_review_changes returned Err: {result:?}"
    );

    let updated = store.get(task_id_num).await.unwrap();

    // When model_for_complexity returns a valid model → Routed, agent preserved.
    // When all models are cooled → falls back to New, agent cleared for full re-route.
    // In test env with no model_map configured, model_for_complexity returns None → New.
    // This is the expected fallback behavior.
    let model_was_resolved = updated.model.as_deref().is_some_and(|m| !m.is_empty());
    if model_was_resolved {
        // Got a valid model → should be Routed, agent preserved
        assert_eq!(
            updated.agent.as_deref(),
            Some("claude"),
            "agent must be preserved when model_for_complexity returns a valid model"
        );
        assert_eq!(
            updated.status,
            TaskStatus::Routed,
            "status must be Routed when model_for_complexity returns a valid model"
        );
    } else {
        // All models cooled or no pools configured → falls back to New, agent cleared
        assert_eq!(
            updated.agent.as_deref(),
            None,
            "agent must be cleared when falling back to full re-route"
        );
        assert_eq!(
            updated.status,
            TaskStatus::New,
            "status must be New when model_for_complexity returns None (all cooled)"
        );
    }

    assert_eq!(
        updated.review_agent_failures, 0,
        "review_agent_failures must be reset to 0 when re-routing after review changes"
    );
}

/// Regression: a transient `update_task_status` failure must NOT
/// cause `handle_review_changes` to return `Err`.  Returning `Err` would
/// propagate to the review subscriber which increments
/// `review_agent_failures` — incorrectly consuming the retry quota even
/// though the review decision itself was already persisted.
///
/// We simulate the failure by giving the `TaskManager` a different repo
/// than the one the task was created in, so `resolve_task_id` returns
/// `None` and `update_task_status` returns `Err` for that internal task.
#[tokio::test]
async fn handle_review_changes_transition_failure_returns_ok() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let task_repo = "owner/repo";

    let task_id_num = store
        .create(&NewTask {
            external_id: None,
            repo: task_repo.to_string(),
            origin: "internal".to_string(),
            title: "Fix bug".to_string(),
            body: "body".to_string(),
            source: "cron".to_string(),
            source_id: "daily".to_string(),
            author: String::new(),
            url: String::new(),
            labels: vec![],
            parent_id: None,
        })
        .await
        .unwrap();
    store
        .update_status(task_id_num, TaskStatus::InReview)
        .await
        .unwrap();

    let task_id_str = format!("internal:{task_id_num}");
    let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);

    // Intentionally wrong repo — causes resolve_task_id to return None,
    // which makes update_task_status return Err for the internal task.
    let wrong_repo = "other/repo";
    let task_manager = Arc::new(TaskManager::with_store(
        Arc::clone(&backend),
        Arc::clone(&store),
        wrong_repo.to_string(),
    ));

    let task = ExternalTask {
        id: ExternalId(task_id_str.clone()),
        title: "Fix bug".to_string(),
        body: "body".to_string(),
        state: "open".to_string(),
        labels: vec![],
        author: String::new(),
        created_at: "2026-01-01T00:00:00Z".to_string(),
        updated_at: "2026-01-01T00:00:00Z".to_string(),
        url: String::new(),
    };

    // The status transition will fail (wrong repo), but handle_review_changes
    // must still return Ok — review context was persisted before the failure.
    let result = handle_review_changes(
        &task,
        "Fix the null pointer on line 10",
        &[],
        &backend,
        task_repo,
        202,
        "claude",
        "sonnet",
        &task_manager,
        &store,
        &router::RouterConfig::default(),
    )
    .await;

    assert!(
        result.is_ok(),
        "handle_review_changes must return Ok even when status transition fails: {result:?}"
    );

    // Review context must have been persisted despite the status failure.
    let stored = store.get(task_id_num).await.unwrap();
    assert!(
        stored
            .pr_review_context
            .contains("Fix the null pointer on line 10"),
        "pr_review_context must be persisted even when status transition fails"
    );
    // Review_cycles should NOT be incremented if the status transition fails.
    // This prevents premature escalation when the Routed transition fails.
    // The increment happens AFTER successful status transition in the fixed code.
    assert_eq!(
        stored.review_cycles, 0,
        "review_cycles must NOT be incremented when status transition fails"
    );
    // Status stays InReview (not Routed/New) because the transition failed.
    assert_eq!(
        stored.status,
        TaskStatus::InReview,
        "status should remain InReview when status transition failed"
    );
}

/// ISO-8601 timestamps returned by GitHub compare lexicographically in
/// chronological order.  `branch_newer_than_last_review` relies on this.
#[test]
fn iso8601_timestamps_compare_correctly() {
    // newer commit (pushed after review) should be greater
    assert!(iso8601_is_newer(
        "2024-01-15T13:00:00Z",
        "2024-01-15T12:30:00Z"
    ));
    // same timestamp is not "newer"
    assert!(!iso8601_is_newer(
        "2024-01-15T12:30:00Z",
        "2024-01-15T12:30:00Z"
    ));
    // older commit is not newer
    assert!(!iso8601_is_newer(
        "2024-01-14T23:59:59Z",
        "2024-01-15T00:00:01Z"
    ));
    // cross-day boundary
    assert!(iso8601_is_newer(
        "2024-01-16T00:00:01Z",
        "2024-01-15T23:59:59Z"
    ));
}

#[test]
fn is_pr_behind_matches_mergeable_state() {
    let mut pr = GitHubPullRequest {
        number: 1,
        title: "Test PR".to_string(),
        body: None,
        state: "open".to_string(),
        head: GitHubBranchRef {
            ref_: "feature/test".to_string(),
            sha: "abc123".to_string(),
            repo: None,
        },
        base: GitHubBranchRef {
            ref_: "main".to_string(),
            sha: "def456".to_string(),
            repo: None,
        },
        mergeable: Some(true),
        mergeable_state: Some("behind".to_string()),
        merged: Some(false),
        html_url: "https://github.com/owner/repo/pull/1".to_string(),
    };

    assert!(is_pr_behind(&pr));

    pr.mergeable_state = Some("clean".to_string());
    assert!(!is_pr_behind(&pr));

    pr.mergeable_state = None;
    assert!(!is_pr_behind(&pr));
}

#[test]
fn fetch_failure_reason_strips_url_credentials() {
    let raw = "fatal: unable to access 'https://x-access-token:test-secret-1234567890abcdef@github.com/o/r.git/'";
    let stderr = git_ops::sanitize_push_error(raw);
    let reason = format!("git fetch failed: {stderr}");
    assert!(!reason.contains("test-secret-1234567890abcdef"));
    assert!(reason.contains("://***@github.com"));
}

#[test]
fn ssh_agent_fetch_failure_matches_sanitized_stderr() {
    let raw = concat!(
        "sign_and_send_pubkey: signing failed: agent refused operation\n",
        "fatal: unable to access 'https://x-access-token:test-secret-1234567890abcdef@github.com/o/r.git/'"
    );
    let sanitized = git_ops::sanitize_push_error(raw);
    assert!(!sanitized.contains("test-secret-1234567890abcdef"));
    assert!(is_ssh_agent_fetch_failure(&sanitized));
    assert!(is_ssh_agent_fetch_failure(raw));
    assert!(matches!(
        classify_fetch_failure(&sanitized),
        WorktreeRebaseOutcome::SshFetchFailed(_)
    ));
}

#[test]
fn classify_merge_failure_treats_out_of_date_head_as_recoverable() {
    let error = r#"GitHub PR merge failed (409 Conflict): {"message":"Head branch is out of date. Review and try the merge again."}"#;
    assert_eq!(
        classify_merge_failure(error),
        MergeFailureClass::RecoverableConflict
    );
}

#[test]
fn classify_merge_failure_treats_moved_head_as_transient() {
    let error = r#"GitHub PR merge failed (409 Conflict): {"message":"Head branch was modified. Review and try the merge again."}"#;
    assert_eq!(classify_merge_failure(error), MergeFailureClass::Transient);
    assert_eq!(
        classify_merge_failure("GitHub PR merge failed (409 Conflict): sha does not match"),
        MergeFailureClass::Transient
    );
}

#[test]
fn classify_merge_failure_treats_gateway_errors_as_transient() {
    assert_eq!(
        classify_merge_failure("GitHub PR merge failed (502 Bad Gateway)"),
        MergeFailureClass::Transient
    );
}

#[test]
fn classify_merge_failure_keeps_branch_protection_errors_blocking() {
    let error = "GitHub PR merge failed (403 Forbidden): branch protection prevented merge";
    assert_eq!(classify_merge_failure(error), MergeFailureClass::Blocking);
}

#[test]
fn classify_merge_failure_treats_draft_as_transient() {
    let error =
        r#"GitHub PR merge failed (405 Method Not Allowed): {"message":"Pull request is a draft"}"#;
    assert_eq!(classify_merge_failure(error), MergeFailureClass::Transient);
}

#[test]
fn classify_merge_failure_keeps_not_mergeable_as_recoverable() {
    let error = r#"GitHub PR merge failed (405 Method Not Allowed): {"message":"Pull Request is not mergeable"}"#;
    assert_eq!(
        classify_merge_failure(error),
        MergeFailureClass::RecoverableConflict
    );
}

#[test]
fn classify_merge_failure_treats_connection_failure_as_transient() {
    let error = "error sending request for url (https://api.github.com/repos/o/r/pulls/1/merge): Failed to connect to github.com:443 after 75021 ms: Could not connect to server";
    assert_eq!(classify_merge_failure(error), MergeFailureClass::Transient);
}

#[test]
fn classify_fetch_failure_treats_connection_timeout_and_ref_lock_as_transient() {
    let connect = concat!(
        "fatal: unable to access 'https://github.com/owner/repo.git/': ",
        "Failed to connect to github.com:443 after 75021 ms: Could not connect to server"
    );
    assert!(matches!(
        classify_fetch_failure(connect),
        WorktreeRebaseOutcome::Transient(_)
    ));
    assert!(matches!(
        classify_fetch_failure("git fetch timed out"),
        WorktreeRebaseOutcome::Transient(_)
    ));
    assert!(matches!(
        classify_fetch_failure("error: cannot lock ref 'refs/remotes/origin/main'"),
        WorktreeRebaseOutcome::Transient(_)
    ));
}

#[tokio::test]
async fn draft_merge_and_connect_fetch_leave_an_approved_task_and_counter() {
    let draft =
        r#"GitHub PR merge failed (405 Method Not Allowed): {"message":"Pull request is a draft"}"#;
    assert_eq!(classify_merge_failure(draft), MergeFailureClass::Transient);

    let fetch = classify_fetch_failure(concat!(
        "fatal: unable to access 'https://github.com/owner/repo.git/': ",
        "Failed to connect to github.com:443 after 75021 ms: Could not connect to server"
    ));
    assert!(
        matches!(fetch, WorktreeRebaseOutcome::Transient(_)),
        "a connect failure must not spend a merge-conflict retry: {fetch:?}"
    );

    let (store, task, task_manager, task_id_num) = approved_task_with_conflict_retries(2).await;
    let gh = GhHttp::new().unwrap();
    apply_rebase_recovery_outcome(
        fetch,
        &task,
        "owner/repo",
        4043,
        &gh,
        &task_manager,
        &store,
        "review-agent",
        "review-model",
    )
    .await
    .unwrap();

    let stored = store.get(task_id_num).await.unwrap();
    assert_eq!(stored.status, TaskStatus::Approved);
    assert_eq!(stored.merge_conflict_retries, 2);
}

#[tokio::test]
async fn auto_merge_pr_refuses_unless_the_task_is_approved() {
    let (store, task, task_manager, task_id_num) = approved_task_with_conflict_retries(0).await;
    store
        .update_status(task_id_num, TaskStatus::InReview)
        .await
        .unwrap();
    let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);
    auto_merge_pr(
        &task,
        "feat-branch",
        &backend,
        "owner/repo",
        "review-agent",
        "review-model",
        &task_manager,
        &store,
    )
    .await
    .unwrap();
    let stored = store.get(task_id_num).await.unwrap();
    assert_eq!(stored.status, TaskStatus::InReview);
    assert_eq!(stored.merge_conflict_retries, 0);
}

#[tokio::test]
async fn apply_rebase_recovery_increments_retries_once_on_content_conflict() {
    let (store, task, task_manager, task_id_num) = approved_task_with_conflict_retries(0).await;
    let gh = GhHttp::new().unwrap();
    apply_rebase_recovery_outcome(
        WorktreeRebaseOutcome::RebaseConflict(
            "CONFLICT (content): merge conflict in src/lib.rs".to_string(),
        ),
        &task,
        "owner/repo",
        4142,
        &gh,
        &task_manager,
        &store,
        "review-agent",
        "review-model",
    )
    .await
    .unwrap();
    let stored = store.get(task_id_num).await.unwrap();
    assert_eq!(stored.status, TaskStatus::Routed);
    assert_eq!(stored.merge_conflict_retries, 1);
}

/// When the GitHub API is unavailable (no real token in unit tests),
/// `handle_review_changes` must still escalate correctly rather than
/// getting stuck.  The stale-review check fails gracefully and we fall
/// through to the normal Blocked path.
#[tokio::test]
async fn handle_review_changes_escalates_when_stale_check_fails() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let repo = "owner/repo";

    let task_id_num = store
        .create(&NewTask {
            external_id: None,
            repo: repo.to_string(),
            origin: "internal".to_string(),
            title: "Fix defaults".to_string(),
            body: "body".to_string(),
            source: "cron".to_string(),
            source_id: "daily".to_string(),
            author: String::new(),
            url: String::new(),
            labels: vec![],
            parent_id: None,
        })
        .await
        .unwrap();

    // Set review_cycles to max so we enter the escalation path.
    store
        .set_fields(task_id_num, &[("review_cycles", serde_json::json!(2i64))])
        .await
        .unwrap();
    store
        .update_status(task_id_num, TaskStatus::InReview)
        .await
        .unwrap();

    let task_id_str = format!("internal:{task_id_num}");
    let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);
    let task_manager = Arc::new(TaskManager::with_store(
        Arc::clone(&backend),
        Arc::clone(&store),
        repo.to_string(),
    ));

    let task = ExternalTask {
        id: ExternalId(task_id_str.clone()),
        title: "Fix defaults".to_string(),
        body: "body".to_string(),
        state: "open".to_string(),
        labels: vec![],
        author: String::new(),
        created_at: "2026-01-01T00:00:00Z".to_string(),
        updated_at: "2026-01-01T00:00:00Z".to_string(),
        url: String::new(),
    };

    // With no real GitHub token the stale check will fail gracefully and
    // we fall through to normal escalation (Blocked status).
    let result = handle_review_changes(
        &task,
        "sync_interval defaults are wrong",
        &[],
        &backend,
        repo,
        303,
        "minimax",
        "minimax-m2",
        &task_manager,
        &store,
        &router::RouterConfig::default(),
    )
    .await;

    assert!(
        result.is_ok(),
        "handle_review_changes must return Ok on escalation path: {result:?}"
    );

    let stored = store.get(task_id_num).await.unwrap();
    assert_eq!(
        stored.status,
        TaskStatus::Blocked,
        "task must be Blocked when max review cycles exceeded and stale check is unavailable"
    );
}

/// Regression test for the bug where `auto_merge_pr`'s pending-CI timeout
/// never fired: elapsed pending time must accumulate in the KV store
/// across separate calls (simulating separate short-lived invocations),
/// not reset to zero each time like a function-local `Instant` would.
#[tokio::test]
async fn ci_pending_elapsed_persists_across_calls() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let task_id = "internal:1";

    // First observation: no prior timestamp, so elapsed starts at zero
    // and the "first seen pending" timestamp is persisted.
    let first = ci_pending_elapsed(&store, REPO, task_id, SHA)
        .await
        .unwrap();
    assert!(
        first.num_seconds() < 1,
        "first pending observation should report ~zero elapsed, got {first:?}"
    );

    // Simulate time passing between two short-lived invocations by
    // backdating the persisted timestamp directly, the way it would look
    // if `is_ci_check_in_cooldown` had short-circuited several polls ago.
    let backdated = chrono::Utc::now() - chrono::Duration::seconds(700);
    store
        .kv_set(
            &ci_pending_since_key(REPO, task_id, SHA),
            &backdated.to_rfc3339(),
        )
        .await
        .unwrap();

    let second = ci_pending_elapsed(&store, REPO, task_id, SHA)
        .await
        .unwrap();
    assert!(
        second.num_seconds() >= 600,
        "elapsed pending time must accumulate across calls instead of resetting, got {second:?}"
    );

    // Clearing removes the persisted timestamp so the next observation
    // starts a fresh streak at ~zero elapsed.
    clear_ci_pending_since(&store, REPO, task_id, "test")
        .await
        .unwrap();
    let after_clear = ci_pending_elapsed(&store, REPO, task_id, SHA)
        .await
        .unwrap();
    assert!(
        after_clear.num_seconds() < 1,
        "elapsed must reset to ~zero after clear_ci_pending_since, got {after_clear:?}"
    );
}

/// Regression test for #3609: `auto_merge_pr` can run as two overlapping
/// invocations for the same task (the review-agent's direct approval call
/// in review.rs is not guarded by the same `auto_merge_in_flight` set as
/// the periodic review_open_prs sweep). A racing call that observes CI as
/// newly pending must not overwrite an already-persisted "since" — doing
/// so silently resets the elapsed clock and the 600s timeout escalation
/// never fires, even though a human-visible log line shows CI pending
/// continuously well past `max_wait`.
#[tokio::test]
async fn ci_pending_elapsed_atomic_insert_survives_concurrent_racers() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let task_id = "internal:2";

    let backdated = chrono::Utc::now() - chrono::Duration::seconds(700);
    store
        .kv_set(
            &ci_pending_since_key(REPO, task_id, SHA),
            &backdated.to_rfc3339(),
        )
        .await
        .unwrap();

    // A second, racing invocation observing the same pending state must
    // read back the already-persisted timestamp instead of clobbering it
    // with its own "now".
    let racer = ci_pending_elapsed(&store, REPO, task_id, SHA)
        .await
        .unwrap();
    assert!(
        racer.num_seconds() >= 600,
        "a racing call must not reset an already-persisted ci_pending_since, got {racer:?}"
    );
}

async fn approved_task_with_conflict_retries(
    retries: i32,
) -> (Arc<TaskStore>, ExternalTask, Arc<TaskManager>, i64) {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let repo = "owner/repo";
    let task_id_num = store
        .create(&NewTask {
            external_id: None,
            repo: repo.to_string(),
            origin: "internal".to_string(),
            title: "Approved merge".to_string(),
            body: "body".to_string(),
            source: "github".to_string(),
            source_id: "4086".to_string(),
            author: String::new(),
            url: String::new(),
            labels: vec![],
            parent_id: None,
        })
        .await
        .unwrap();
    store
        .update_status(task_id_num, TaskStatus::Approved)
        .await
        .unwrap();
    for _ in 0..retries {
        store
            .increment(task_id_num, "merge_conflict_retries")
            .await
            .unwrap();
    }

    let task_id_str = format!("internal:{task_id_num}");
    let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);
    let task_manager = Arc::new(TaskManager::with_store(
        Arc::clone(&backend),
        Arc::clone(&store),
        repo.to_string(),
    ));
    let task = ExternalTask {
        id: ExternalId(task_id_str),
        title: "Approved merge".to_string(),
        body: "body".to_string(),
        state: "open".to_string(),
        labels: vec![],
        author: String::new(),
        created_at: "2026-01-01T00:00:00Z".to_string(),
        updated_at: "2026-01-01T00:00:00Z".to_string(),
        url: String::new(),
    };
    (store, task, task_manager, task_id_num)
}

const REPO: &str = "owner/repo";
const SHA: &str = "abc123";

#[tokio::test]
async fn ci_pending_window_is_per_repo_and_per_head() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let backdated = chrono::Utc::now() - chrono::Duration::days(3);
    store
        .kv_set(
            &ci_pending_since_key("other/repo", "7", SHA),
            &backdated.to_rfc3339(),
        )
        .await
        .unwrap();
    store
        .kv_set(
            &ci_pending_since_key(REPO, "7", "oldsha"),
            &backdated.to_rfc3339(),
        )
        .await
        .unwrap();

    let other_repo = ci_pending_elapsed(&store, REPO, "7", SHA).await.unwrap();
    assert!(other_repo.num_seconds() < 1, "got {other_repo:?}");

    let new_head = ci_pending_elapsed(&store, REPO, "7", "newsha")
        .await
        .unwrap();
    assert!(new_head.num_seconds() < 1, "got {new_head:?}");

    clear_ci_pending_since(&store, REPO, "7", "test")
        .await
        .unwrap();
    assert_eq!(
        store
            .kv_list_prefix(&ci_pending_since_prefix(REPO, "7"))
            .await
            .unwrap(),
        [] as [(String, String); 0]
    );

    assert!(
        store
            .kv_get(&ci_pending_since_key("other/repo", "7", SHA))
            .await
            .unwrap()
            .is_some()
    );
}

#[tokio::test]
async fn ci_check_cooldown_is_per_repo() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    record_ci_check(&store, "a/x", "5").await.unwrap();
    assert!(is_ci_check_in_cooldown(&store, "a/x", "5").await.unwrap());
    assert!(!is_ci_check_in_cooldown(&store, "b/x", "5").await.unwrap());
}

/// A get_pr failure after get_closed_pr_state reported a match must not mark the task done
#[expect(
    clippy::await_holding_lock,
    reason = "the env lock stays held across the awaits so tests do not race on GH_TOKEN"
)]
#[tokio::test]
async fn finish_already_closed_pr_leaves_status_when_get_pr_fails() {
    let _env_guard = token::ENV_LOCK.lock().unwrap();
    // SAFETY: nextest runs each test in its own process, so no other test touches the environment
    unsafe { env::set_var("GH_TOKEN", "test_token") };

    let server = MockServer::start().await;
    let repo = "owner/repo";
    let branch = "task-branch";

    Mock::given(method("GET"))
        .and(path("/repos/owner/repo/pulls"))
        .and(query_param("state", "closed"))
        .and(query_param("head", "owner:task-branch"))
        .respond_with(
            ResponseTemplate::new(200).set_body_json(serde_json::json!([{
                "number": 42,
                "state": "closed",
                "merged_at": "2026-01-01T00:00:00Z",
                "head": { "ref": "task-branch" }
            }])),
        )
        .mount(&server)
        .await;
    Mock::given(method("GET"))
        .and(path("/repos/owner/repo/pulls/42"))
        .respond_with(ResponseTemplate::new(404).set_body_string("Not Found"))
        .mount(&server)
        .await;

    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let task_id_num = store
        .create(&NewTask {
            repo: repo.to_string(),
            origin: "internal".to_string(),
            title: "task".to_string(),
            ..Default::default()
        })
        .await
        .unwrap();
    store
        .update_status(task_id_num, TaskStatus::InReview)
        .await
        .unwrap();

    let backend: Arc<dyn ExternalBackend> = Arc::new(NoopBackend);
    let task_manager = Arc::new(TaskManager::with_store(
        Arc::clone(&backend),
        Arc::clone(&store),
        repo.to_string(),
    ));
    let task = ExternalTask {
        id: ExternalId(format!("internal:{task_id_num}")),
        title: "task".to_string(),
        body: String::new(),
        state: "open".to_string(),
        labels: vec![],
        author: String::new(),
        created_at: "2026-01-01T00:00:00Z".to_string(),
        updated_at: "2026-01-01T00:00:00Z".to_string(),
        url: String::new(),
    };

    let gh = GhHttp::for_test(server.uri(), None);
    let result = finish_already_closed_pr(&gh, &task, branch, repo, &task_manager, &store).await;
    assert!(
        result.is_err(),
        "get_pr failure must not finish the task: {result:?}"
    );
    let stored = store.get(task_id_num).await.unwrap();
    assert_eq!(stored.status, TaskStatus::InReview);
}

#[tokio::test]
async fn sweep_removes_finished_task_and_legacy_keys() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    record_ci_check(&store, REPO, "7").await.unwrap();
    record_ci_check(&store, REPO, "8").await.unwrap();
    store
        .kv_set(&ci_pending_since_key(REPO, "7", SHA), "t")
        .await
        .unwrap();
    store
        .kv_set(&ci_failure_sha_key(REPO, "7", SHA), "1")
        .await
        .unwrap();
    store
        .kv_set(&ci_failure_sha_key(REPO, "8", SHA), "1")
        .await
        .unwrap();
    store.kv_set("ci_check_ts:7", "t").await.unwrap();
    store.kv_set("ci_pending_since:7", "t").await.unwrap();
    store
        .kv_set(&format!("ci_bootstrap_wait_since:{REPO}#7#abc"), "t")
        .await
        .unwrap();
    store
        .kv_set(&format!("ci_bootstrap_wait_since:{REPO}#8#abc"), "t")
        .await
        .unwrap();

    sweep_ci_timer_keys(&store, REPO, &["7".to_string()])
        .await
        .unwrap();

    assert!(
        store
            .kv_get(&ci_check_ts_key(REPO, "7"))
            .await
            .unwrap()
            .is_none()
    );
    assert_eq!(
        store
            .kv_list_prefix(&ci_pending_since_prefix(REPO, "7"))
            .await
            .unwrap(),
        [] as [(String, String); 0]
    );

    assert!(store.kv_get("ci_check_ts:7").await.unwrap().is_none());
    assert!(store.kv_get("ci_pending_since:7").await.unwrap().is_none());
    assert!(
        store
            .kv_get(&ci_check_ts_key(REPO, "8"))
            .await
            .unwrap()
            .is_some()
    );
    assert!(
        store
            .kv_get(&ci_failure_sha_key(REPO, "7", SHA))
            .await
            .unwrap()
            .is_none()
    );
    assert!(
        store
            .kv_get(&ci_failure_sha_key(REPO, "8", SHA))
            .await
            .unwrap()
            .is_some()
    );
    assert!(
        store
            .kv_get(&format!("ci_bootstrap_wait_since:{REPO}#7#abc"))
            .await
            .unwrap()
            .is_none()
    );
    assert!(
        store
            .kv_get(&format!("ci_bootstrap_wait_since:{REPO}#8#abc"))
            .await
            .unwrap()
            .is_some()
    );
}

async fn task_for_ci_failure(repo: &str, source_id: &str) -> (Arc<TaskStore>, String, i64) {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let store_id = store
        .create_internal(repo, "CI failure", "", "test", source_id, None)
        .await
        .unwrap();
    (store, format!("internal:{store_id}"), store_id)
}

#[tokio::test]
async fn count_ci_failure_for_head_counts_a_sha_once() {
    let (store, task_id, store_id) = task_for_ci_failure(REPO, "once").await;

    let first = count_ci_failure_for_head(&store, REPO, &task_id, SHA)
        .await
        .unwrap();
    let again = count_ci_failure_for_head(&store, REPO, &task_id, SHA)
        .await
        .unwrap();
    let next = count_ci_failure_for_head(&store, REPO, &task_id, "newsha")
        .await
        .unwrap();

    assert_eq!(first, Some(1));
    assert_eq!(again, None);
    assert_eq!(next, Some(2));
    assert_eq!(store.get(store_id).await.unwrap().ci_merge_failures, 2);
}

#[tokio::test]
async fn count_ci_failure_for_head_is_per_task() {
    let store = Arc::new(TaskStore::open_memory().await.unwrap());
    let a = store
        .create_internal(REPO, "a", "", "test", "a", None)
        .await
        .unwrap();
    let b = store
        .create_internal(REPO, "b", "", "test", "b", None)
        .await
        .unwrap();
    let a_id = format!("internal:{a}");
    let b_id = format!("internal:{b}");

    assert_eq!(
        count_ci_failure_for_head(&store, REPO, &a_id, SHA)
            .await
            .unwrap(),
        Some(1)
    );
    assert_eq!(
        count_ci_failure_for_head(&store, REPO, &b_id, SHA)
            .await
            .unwrap(),
        Some(1)
    );
    assert_eq!(store.get(a).await.unwrap().ci_merge_failures, 1);
    assert_eq!(store.get(b).await.unwrap().ci_merge_failures, 1);
}

#[tokio::test]
async fn count_ci_failure_for_head_empty_sha_always_counts() {
    let (store, task_id, store_id) = task_for_ci_failure(REPO, "empty").await;

    assert_eq!(
        count_ci_failure_for_head(&store, REPO, &task_id, "")
            .await
            .unwrap(),
        Some(1)
    );
    assert_eq!(
        count_ci_failure_for_head(&store, REPO, &task_id, "")
            .await
            .unwrap(),
        Some(2)
    );
    assert_eq!(store.get(store_id).await.unwrap().ci_merge_failures, 2);
}

#[tokio::test]
async fn count_ci_failure_for_head_only_one_racer_counts() {
    let (store, task_id, store_id) = task_for_ci_failure(REPO, "race").await;

    let (a, b) = tokio::join!(
        count_ci_failure_for_head(&store, REPO, &task_id, SHA),
        count_ci_failure_for_head(&store, REPO, &task_id, SHA),
    );
    let counted = [a.unwrap(), b.unwrap()].into_iter().flatten().count();
    assert_eq!(counted, 1);
    assert_eq!(store.get(store_id).await.unwrap().ci_merge_failures, 1);
}