car-server-core 0.55.0

Transport-neutral library for the CAR daemon JSON-RPC dispatcher (used by car-server and tokhn-daemon)
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
//! One pass of the self-healing loop.
//!
//! T5 of `docs/proposals/self-healing-issue-loop.md`. Composes the pieces:
//! read → select → claim → hand to the coder → gate → open a pull request, or
//! give the item back.
//!
//! ## At most one item, then exit
//!
//! A tick is not a loop. It takes one item and returns, and the *scheduler*
//! decides when to run again. That is what makes the thing interruptible: a
//! daemon restart loses at most one in-flight item, whose claim then expires,
//! rather than a long-running process holding an unbounded amount of work.
//!
//! ## Idle is the steady state
//!
//! The queue is human-authored. A tick that finds nothing eligible has not
//! failed — humans control what gets labelled, and most of the time the honest
//! answer is that there is nothing to do. [`TickOutcome::Idle`] carries the
//! skip reasons so an operator can tell "nothing eligible" from "everything was
//! skipped for a reason I did not expect", which is the difference between a
//! quiet loop and a broken one.
//!
//! ## The loop never decides an issue is fixed
//!
//! It opens a pull request referencing the item, and stops. The item stays open
//! until a human closes it through the normal merge path. So there is no "am I
//! done" judgement for a model to get wrong — termination is external, by
//! construction.

use std::sync::Arc;

use super::heal_claims::ClaimStore;
use super::heal_gate::{decide, GateOutcome, Unreachable, Verdict};
use super::heal_intake::{Checkout, HealTarget};
use super::heal_select::{select, Candidate, Selection, SkippedItem};
use super::merge::{CiSummary, PrDeliveryOutcome};
use super::provenance::{ProvenanceTier, SessionSeed};

/// What resolving an item's provenance produced.
///
/// Three outcomes, not two, because `Option` conflated states whose correct
/// handling differs. An item that vanished between the scan and now needs no
/// backoff — it is gone from the queue and a later tick will not see it. An
/// item whose author the tier gate REFUSED is still in the queue, still the
/// oldest, and still labelled, so without a recorded failure selection picks it
/// again on every tick and halts the sweep there forever — starving everything
/// behind it. That path became reachable the moment selection stopped
/// rejecting untiered candidates outright.
#[derive(Debug)]
pub enum Intent {
    /// Cleared. The text may seed a coder session.
    Seed(SessionSeed),
    /// Closed or edited between the scan and now. The normal case for a
    /// human-authored queue, and not a failure of anything.
    Gone,
    /// The provenance gate said no.
    ///
    /// `stale` separates the two refusals, because their remedies differ and
    /// reporting one as the other sends an operator to the wrong place: a tier
    /// resolved too long ago is fixed by retrying, an untrusted author is not
    /// fixed by anything the operator does to this daemon.
    Refused { tier: ProvenanceTier, stale: bool },
}

/// A coder run that failed, and the session it left behind.
///
/// The session id is the load-bearing half. `run` can fail *after* the session
/// has reached `NeedsApproval` — no worktree at approval, an unreadable diff, a
/// green contract over an unchanged worktree — and a plain `String` gave the
/// caller no way to close that session out. Each one leaked a git worktree
/// registered in the operator's own repository, with its task handle already
/// taken so `coder.cancel` could not reach it either, and the reaper that would
/// have swept them was deleted on the strength of the invariant these paths
/// break.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunFailure {
    pub detail: String,
    /// `None` only when the failure happened before a session existed.
    pub session_id: Option<String>,
    /// The daemon configuration prevented any model from attempting the item.
    /// Such a failure must not create item backoff or a public issue comment.
    pub configuration: bool,
}

impl RunFailure {
    /// A failure with no session behind it — nothing to close out.
    pub fn early(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
            session_id: None,
            configuration: false,
        }
    }

    /// A failure that left a live session.
    pub fn with_session(session_id: &str, detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
            session_id: Some(session_id.to_string()),
            configuration: false,
        }
    }

    /// A session stopped before model dispatch because its routing constraints
    /// leave no independent coder available.
    pub fn configuration_with_session(session_id: &str, detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
            session_id: Some(session_id.to_string()),
            configuration: true,
        }
    }
}

/// Why a delivery stopped, and whether trying again could ever change it.
///
/// `permanent` is not a hint. A closed pull request on the item's delivery
/// branch is a person saying stop, and `merge::deliver_pr_with` refuses it at
/// preflight every time — so an ordinary failure record would spend four more
/// coder sessions and four more panel fan-outs rediscovering the same answer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeliverRefusal {
    pub detail: String,
    pub permanent: bool,
}

impl DeliverRefusal {
    pub fn retriable(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
            permanent: false,
        }
    }
    pub fn permanent(detail: impl Into<String>) -> Self {
        Self {
            detail: detail.into(),
            permanent: true,
        }
    }
}

/// What a coder run produced: the facts a gate decides over.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Attempt {
    /// Whether the outcome contract passed. The deterministic half.
    pub contract_passed: bool,
    /// What the contract reported, for the operator and the reviewers.
    pub contract_detail: String,
    /// How many reviewers were ASKED. The threshold derives from this, not
    /// from how many answered.
    pub panel_size: usize,
    pub verdicts: Vec<Verdict>,
    pub unreachable: Vec<Unreachable>,
    /// The coder session that produced the work, still holding its worktree.
    ///
    /// A session id rather than a branch, because **nothing is published until
    /// the gate approves**. In the interactive path `car/coder/<id>` is created
    /// by `coder.approve_merge` — the branch exists only after a human said
    /// yes, so it means "approved". Publishing before the panel would put
    /// unapproved work in a namespace that already carries that meaning, and
    /// leave one dead branch per rejection in the operator's repository. The
    /// panel reviews the worktree diff instead, which is the same view the
    /// human approval surface shows.
    pub session_id: String,
}

/// What one tick did.
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
#[serde(tag = "outcome", rename_all = "snake_case")]
pub enum TickOutcome {
    /// Nothing was eligible. The reasons are carried so a quiet loop can be
    /// told apart from a stuck one.
    Idle { skipped: Vec<SkippedItem> },
    /// A pull request was opened for the item.
    Opened {
        repo: String,
        number: u64,
        pr_url: String,
        gate: String,
        /// Typed check-run/combined-status state for the delivered head SHA.
        ci: CiSummary,
        /// Human-readable form of `ci`, suitable for a delivery log.
        delivery: String,
    },
    /// The item was attempted and did not clear the gate. The claim is
    /// released; the item returns to the queue for a human or a later tick.
    Rejected {
        repo: String,
        number: u64,
        gate: String,
    },
    /// The tick could not run at all — the target is unusable, or a read
    /// failed. Distinct from `Idle` because the remedy is different.
    Failed { detail: String },
}

/// Everything a tick needs from the outside world, injected.
///
/// A trait rather than concrete calls so the composition — the order of claim,
/// work, gate, release — is testable without GitHub, inference, or a worktree.
/// That order is the part most likely to be wrong, and the part a live test
/// would exercise least reliably.
#[async_trait::async_trait]
pub trait TickIo: Send + Sync {
    /// Candidates on this target, already tiered and label-checked.
    async fn candidates(&self, target: &HealTarget) -> Result<Vec<Candidate>, String>;
    /// Open pull requests on this target, for the coverage check.
    async fn open_prs(
        &self,
        target: &HealTarget,
    ) -> Result<Vec<super::heal_intake::RawPullRequest>, String>;
    /// The trust-cleared seed for this item, or `None` if it cannot be cleared.
    ///
    /// [`SessionSeed`] and not `String`: the newtype exists precisely so cleared
    /// text cannot be moved around as an ordinary string, and returning a
    /// `String` here handed that guarantee back. `None` means the item must be
    /// skipped — never "use the raw body instead".
    async fn intent_for(&self, item: &Candidate) -> Result<Intent, String>;
    /// Run a coder session and return the EVIDENCE, never the verdict.
    ///
    /// An earlier shape had this return a finished `GateOutcome`, which meant
    /// the injected implementation could hand back `Approved` having run no
    /// contract and asked no reviewer — and `decide` had no callers at all. A
    /// gate a caller can mint is not a gate. `GateOutcome` is now sealed so
    /// only [`super::heal_gate::decide`] can build one, and this returns the
    /// inputs to it.
    async fn run_coder(
        &self,
        target: &HealTarget,
        item: &Candidate,
        intent: &SessionSeed,
    ) -> Result<Attempt, RunFailure>;
    /// Redact text bound for a public tracker.
    ///
    /// Gate summaries carry contract output and vendor error strings, which is
    /// where hostnames and credentials surface. The target may be a public
    /// repository, and a comment cannot be unposted.
    fn redact(&self, text: &str) -> String;
    /// Publish the work and open the pull request, as one step.
    ///
    /// Called only after [`decide`] approves — this is the loop's single write
    /// to the world, and everything before it is reversible by doing nothing.
    /// The outcome includes check-run/combined-status state read for the exact
    /// delivered head SHA.
    async fn deliver(
        &self,
        target: &HealTarget,
        item: &Candidate,
        session_id: &str,
        gate: &GateOutcome,
    ) -> Result<PrDeliveryOutcome, DeliverRefusal>;

    /// Close out a session whose work will not be delivered.
    ///
    /// Required rather than defaulted: a coder session holds a git worktree
    /// registered in the operator's own repository until it reaches a terminal
    /// state, and rejection is the *expected* common outcome of a strict
    /// majority. An implementation that forgets this leaks a worktree per
    /// rejected item, forever, and a default no-op would let it.
    async fn abandon(&self, session_id: &str);
    /// Say on the item why the loop stopped, so a human is not left guessing.
    async fn comment(&self, item: &Candidate, text: &str) -> Result<(), String>;
    fn now_ms(&self) -> u64;
}

/// Run one tick against one target.
/// Where the claim ledger is written, and when.
///
/// Injected rather than done by the caller after `tick` returns, because
/// *after* is too late: a claim that reaches disk only once the whole tick has
/// finished is lost if the daemon dies during the 45-minute coder session it
/// was taken for, and the next start re-picks an item whose work is already in
/// flight — possibly still in flight, in a detached session. `heal_service`'s
/// own module docs state this contract; nothing implemented it.
pub trait ClaimSink: Send + Sync {
    fn persist(&self, claims: &ClaimStore, now_ms: u64);
}

/// A sink that writes nothing — for tests, and for a caller with no state dir.
pub struct NoClaimSink;
impl ClaimSink for NoClaimSink {
    fn persist(&self, _claims: &ClaimStore, _now_ms: u64) {}
}

pub async fn tick(
    io: &Arc<dyn TickIo>,
    target: &HealTarget,
    claims: &mut ClaimStore,
    run_id: &str,
    sink: &dyn ClaimSink,
) -> TickOutcome {
    // A target that cannot be written to is watch-only. Establish that before
    // spending any API call: reading a queue we can do nothing about is pure
    // cost.
    if !target.can_write() {
        // Reported, not silent: `heal_config` promises a watch-only target is a
        // legal configuration, so an operator must be able to see that is why
        // nothing happened.
        return TickOutcome::Idle {
            skipped: vec![SkippedItem {
                repo: target.repo.clone(),
                number: 0,
                reason: super::heal_select::Skip::WatchOnly,
            }],
        };
    }
    if !super::heal_intake::is_valid_repo_spec(&target.repo) {
        return TickOutcome::Failed {
            detail: format!("`{}` is not a valid owner/name spec", target.repo),
        };
    }

    let now = io.now_ms();
    let candidates = match io.candidates(target).await {
        Ok(c) => c,
        Err(e) => return TickOutcome::Failed { detail: e },
    };
    let prs = match io.open_prs(target).await {
        Ok(p) => p,
        Err(e) => return TickOutcome::Failed { detail: e },
    };

    let Selection { chosen, skipped } = select(
        target,
        &candidates,
        &prs,
        claims.as_map(),
        claims.attempts(),
        now,
    );
    let Some(item) = chosen else {
        return TickOutcome::Idle { skipped };
    };

    // Claim BEFORE any work. The reverse order — work, then claim — leaves a
    // window where a second tick starts the same item, which is the whole thing
    // claiming exists to prevent.
    // Persisted immediately below, before the first thing that can take
    // minutes. See `ClaimSink`.
    if let Err(refused) = claims.claim(&item.repo, item.number, run_id, now) {
        return TickOutcome::Idle {
            skipped: vec![SkippedItem {
                repo: item.repo.clone(),
                number: item.number,
                reason: super::heal_select::Skip::Claimed {
                    run_id: refused.held_by,
                },
            }],
        };
    }
    sink.persist(claims, now);

    // From here every exit must release the claim, or a failed tick parks the
    // item until its TTL expires.
    let intent = match io.intent_for(&item).await {
        Ok(Intent::Seed(i)) => i,
        Ok(Intent::Gone) => {
            // The queue moved. No failure recorded: there is nothing to back
            // off from, and a later tick will not see this item at all.
            claims.release(&item.repo, item.number, run_id);
            return TickOutcome::Idle {
                skipped: vec![SkippedItem {
                    repo: item.repo.clone(),
                    number: item.number,
                    reason: super::heal_select::Skip::Gone,
                }],
            };
        }
        Ok(Intent::Refused { tier, stale }) => {
            // Recorded, or the sweep halts here forever. This item is still in
            // the queue, still the oldest, and still labelled, so selection
            // picks it again on the next tick and every tick after — starving
            // everything behind it while the operator sees only "not cleared".
            // The backoff is what moves the queue past it.
            // A stale tier is fixed by retrying; an untrusted author is not
            // fixed by anything done to this daemon, so it exhausts at once
            // rather than costing 1+1+2+4+8 hours of permission calls to reach
            // the same answer five times.
            if stale {
                claims.record_failure(
                    &item.repo,
                    item.number,
                    "provenance was resolved too long ago to rely on",
                    now,
                );
            } else {
                claims.record_permanent_failure(
                    &item.repo,
                    item.number,
                    &format!("author is not cleared to seed a session (tier: {tier:?})"),
                    now,
                );
            }
            claims.release(&item.repo, item.number, run_id);
            return TickOutcome::Idle {
                skipped: vec![SkippedItem {
                    repo: item.repo.clone(),
                    number: item.number,
                    // A stale tier is NOT an authorisation failure. Reporting
                    // it as one sends an operator to check permissions when
                    // the remedy is to retry.
                    reason: if stale {
                        super::heal_select::Skip::IntentNotCleared
                    } else {
                        super::heal_select::Skip::UntrustedAuthor { tier }
                    },
                }],
            };
        }
        Err(e) => {
            claims.release(&item.repo, item.number, run_id);
            return TickOutcome::Failed { detail: e };
        }
    };

    let attempt = match io.run_coder(target, &item, &intent).await {
        Ok(v) => v,
        Err(failure) => {
            // Close the session out if one was left. `run` can fail after the
            // session reached `NeedsApproval`, and a non-terminal session holds
            // a git worktree in the operator's repository until something
            // transitions it.
            if let Some(id) = &failure.session_id {
                io.abandon(id).await;
            }
            let detail = failure.detail;
            if failure.configuration {
                // Nothing about this item failed: the daemon excluded every
                // review seat and had no independent coder left. Recording
                // item backoff or posting on the issue would blame the backlog
                // for a heal.toml condition only the operator can change.
                claims.release(&item.repo, item.number, run_id);
                return TickOutcome::Failed { detail };
            }
            claims.record_failure(&item.repo, item.number, &detail, now);
            claims.release(&item.repo, item.number, run_id);
            let _ = io
                .comment(
                    &item,
                    &io.redact(&format!("self-heal could not run: {detail}")),
                )
                .await;
            return TickOutcome::Failed { detail };
        }
    };

    // The verdict is computed HERE, from evidence, by the one function that
    // can build a `GateOutcome`.
    let gate = decide(
        attempt.contract_passed,
        &attempt.contract_detail,
        attempt.panel_size,
        &attempt.verdicts,
        &attempt.unreachable,
    );

    if !gate.approved() {
        let summary = gate.summary();
        // Terminal, so the RAII handle drops and the git worktree registered in
        // the operator's repository goes with it. Before the comment, because
        // the comment is best-effort and the worktree is not.
        io.abandon(&attempt.session_id).await;
        // Record the failure BEFORE releasing, or the next tick picks the same
        // item immediately and the loop becomes a metronome.
        claims.record_failure(&item.repo, item.number, &summary, now);
        claims.release(&item.repo, item.number, run_id);
        // Say why on the item itself, redacted: an unattended loop that fails
        // silently teaches people to ignore it, and one that leaks a vendor
        // error string into a public tracker cannot be unposted.
        let _ = io
            .comment(&item, &io.redact(&format!("self-heal stopped: {summary}")))
            .await;
        return TickOutcome::Rejected {
            repo: item.repo.clone(),
            number: item.number,
            gate: summary,
        };
    }

    match io.deliver(target, &item, &attempt.session_id, &gate).await {
        Ok(delivered) => {
            // The work landed. Forget prior failures so a later, unrelated
            // failure starts from a clean backoff rather than an old count.
            claims.clear_failures(&item.repo, item.number);
            let delivery = delivered.delivery_report();
            TickOutcome::Opened {
                repo: item.repo.clone(),
                number: item.number,
                pr_url: delivered.pr_url,
                gate: gate.summary(),
                ci: delivered.ci,
                delivery,
            }
        }
        Err(refusal) => {
            io.abandon(&attempt.session_id).await;
            let detail = format!(
                "gate passed but the pull request could not be opened: {}",
                refusal.detail
            );
            // A refusal nothing can change exhausts the item now rather than
            // four sessions from now. See `DeliverRefusal`.
            if refusal.permanent {
                claims.record_permanent_failure(&item.repo, item.number, &detail, now);
            } else {
                claims.record_failure(&item.repo, item.number, &detail, now);
            }
            claims.release(&item.repo, item.number, run_id);
            // The case a human most needs told, so it comments like every other
            // stop rather than failing silently.
            let _ = io.comment(&item, &io.redact(&detail)).await;
            TickOutcome::Failed { detail }
        }
    }
}

/// The checkout argument a coder session takes for this target.
pub fn coder_target(target: &HealTarget) -> Option<(Option<std::path::PathBuf>, Option<String>)> {
    match target.checkout.as_ref()? {
        Checkout::Local(p) => Some((Some(p.clone()), None)),
        Checkout::Project(slug) => Some((None, Some(slug.clone()))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::coder::heal_intake::{ChecksState, RawPullRequest};
    use crate::coder::heal_select::{CandidateKind, CLAIM_TTL_MS};
    use crate::coder::provenance::ProvenanceTier;
    use std::sync::Mutex;

    #[derive(Default)]
    struct Fake {
        candidates: Vec<Candidate>,
        prs: Vec<RawPullRequest>,
        intent: Option<String>,
        intent_err: Option<String>,
        attempt: Option<Attempt>,
        coder_err: Option<String>,
        coder_configuration_error: bool,
        pr_err: Option<String>,
        ci_unavailable: bool,
        calls: Mutex<Vec<String>>,
        /// The fake's clock. Larger than `CLAIM_TTL_MS` so a test can express
        /// "claimed before the window" without underflowing.
        now: u64,
        /// Which refusal `intent: None` stands for: a tier resolved too long
        /// ago (retry works) rather than an untrusted author (it does not).
        intent_stale: bool,
    }

    #[async_trait::async_trait]
    impl TickIo for Fake {
        async fn candidates(&self, _t: &HealTarget) -> Result<Vec<Candidate>, String> {
            self.calls.lock().unwrap().push("candidates".into());
            Ok(self.candidates.clone())
        }
        async fn open_prs(&self, _t: &HealTarget) -> Result<Vec<RawPullRequest>, String> {
            Ok(self.prs.clone())
        }
        async fn intent_for(&self, _i: &Candidate) -> Result<Intent, String> {
            self.calls.lock().unwrap().push("intent".into());
            if let Some(e) = &self.intent_err {
                return Err(e.clone());
            }
            Ok(match self.intent.clone() {
                Some(text) => Intent::Seed(SessionSeed::from_trusted(text)),
                // `None` is a gate refusal — `Gone` is the other, distinct
                // case, and `intent_stale` picks which refusal.
                None => Intent::Refused {
                    tier: if self.intent_stale {
                        ProvenanceTier::Maintainer
                    } else {
                        ProvenanceTier::Public
                    },
                    stale: self.intent_stale,
                },
            })
        }

        fn redact(&self, text: &str) -> String {
            text.replace("sk-secret", "[redacted]")
        }
        async fn run_coder(
            &self,
            _t: &HealTarget,
            _i: &Candidate,
            _intent: &SessionSeed,
        ) -> Result<Attempt, RunFailure> {
            self.calls.lock().unwrap().push("coder".into());
            if let Some(e) = &self.coder_err {
                // The fake's coder failure names a session, so the tick's
                // abandon path is exercised rather than skipped.
                return Err(if self.coder_configuration_error {
                    RunFailure::configuration_with_session("coder-fake", e.clone())
                } else {
                    RunFailure::with_session("coder-fake", e.clone())
                });
            }
            Ok(self.attempt.clone().unwrap_or(Attempt {
                contract_passed: true,
                contract_detail: "green".into(),
                panel_size: 3,
                verdicts: vec![
                    Verdict {
                        model: "a".into(),
                        pass: true,
                        reason: "ok".into(),
                    },
                    Verdict {
                        model: "b".into(),
                        pass: true,
                        reason: "ok".into(),
                    },
                ],
                unreachable: vec![],
                session_id: "coder-e2e-1".into(),
            }))
        }
        async fn deliver(
            &self,
            _t: &HealTarget,
            _i: &Candidate,
            _s: &str,
            _g: &GateOutcome,
        ) -> Result<PrDeliveryOutcome, DeliverRefusal> {
            self.calls.lock().unwrap().push("deliver".into());
            if let Some(e) = &self.pr_err {
                return Err(DeliverRefusal::retriable(e.clone()));
            }
            let mut out = fake_delivery();
            if self.ci_unavailable {
                out.ci.state = super::super::merge::CiState::Pending;
                out.ci.checks.clear();
                out.ci.observation_error = Some("HTTP 503".into());
            }
            Ok(out)
        }
        async fn abandon(&self, _s: &str) {
            self.calls.lock().unwrap().push("abandon".into());
        }
        async fn comment(&self, _i: &Candidate, _t: &str) -> Result<(), String> {
            self.calls.lock().unwrap().push("comment".into());
            Ok(())
        }
        fn now_ms(&self) -> u64 {
            if self.now == 0 {
                10 * CLAIM_TTL_MS
            } else {
                self.now
            }
        }
    }

    fn target() -> HealTarget {
        HealTarget {
            repo: "acme/widgets".into(),
            fix_repo: None,
            checkout: Some(Checkout::Project("widgets".into())),
            label: "self-heal".into(),
            base: "main".into(),
        }
    }

    fn item() -> Candidate {
        Candidate {
            repo: "acme/widgets".into(),
            number: 5,
            tier: Some(ProvenanceTier::Maintainer),
            labelled: true,
            created_ms: 0,
            kind: CandidateKind::Issue,
        }
    }

    /// Keep a typed handle AND a trait object over the same fake, so a test can
    /// assert on recorded calls without casting a `dyn` pointer back.
    fn io(f: Fake) -> (Arc<Fake>, Arc<dyn TickIo>) {
        let typed = Arc::new(f);
        let dynamic: Arc<dyn TickIo> = typed.clone();
        (typed, dynamic)
    }

    fn calls_of(f: &Arc<Fake>) -> Vec<String> {
        f.calls.lock().unwrap().clone()
    }

    fn fake_delivery() -> PrDeliveryOutcome {
        PrDeliveryOutcome {
            branch: "self-heal/5".into(),
            commit: "head123".into(),
            pushed: true,
            pr_number: 1,
            pr_url: "https://example/pr/1".into(),
            pr_action: super::super::merge::PrAction::Opened,
            draft: false,
            ci: CiSummary {
                observation_error: None,
                head_sha: "head123".into(),
                state: super::super::merge::CiState::Green,
                checks: vec![
                    super::super::merge::CiCheck {
                        name: "lint".into(),
                        state: super::super::merge::CiState::Green,
                    },
                    super::super::merge::CiCheck {
                        name: "test".into(),
                        state: super::super::merge::CiState::Green,
                    },
                ],
            },
        }
    }

    /// THE starvation regression, and it was opened by the tier fix itself.
    ///
    /// Before untiered candidates were allowed past selection, nothing reached
    /// `intent_for` and this path was dead. Now every labelled issue reaches
    /// it, and a refusal used to release the claim and record nothing — so the
    /// oldest labelled item with an untrusted author (the normal case in a
    /// public repo: a maintainer labels a contributor's bug report) was chosen
    /// on every tick, refused on every tick, and halted the sweep there. Every
    /// item behind it starved forever.
    #[tokio::test]
    async fn a_refused_author_is_recorded_so_the_queue_can_move_past_it() {
        let mut older = item();
        older.number = 1;
        older.created_ms = 1;
        let (_, f) = io(Fake {
            candidates: vec![older],
            // The fake's `None` intent is a gate refusal.
            intent: None,
            now: 1_000_000,
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&f, &target(), &mut claims, "run-1", &NoClaimSink).await;

        match out {
            TickOutcome::Idle { skipped } => assert!(matches!(
                skipped[0].reason,
                super::super::heal_select::Skip::UntrustedAuthor { .. }
            )),
            other => panic!("expected idle, got {other:?}"),
        }
        assert_eq!(claims.held_by("acme/widgets", 1), None);
        assert!(!claims.attempts().is_empty(), "no failure was recorded");

        // THE assertion that matters, and the one the first version of this
        // test was missing: it checked only that the WRITE happened. The
        // attempt ledger was written by `tick` and read by nobody —
        // `select` never received it and `Skip::RecentlyFailed` was
        // constructed nowhere in production — so the very next tick chose the
        // same item again and the sweep never advanced. Proving a backoff
        // requires ticking twice.
        let out2 = tick(&f, &target(), &mut claims, "run-2", &NoClaimSink).await;
        match out2 {
            TickOutcome::Idle { skipped } => assert!(
                matches!(
                    skipped[0].reason,
                    super::super::heal_select::Skip::RecentlyFailed { .. }
                ),
                "the second tick re-selected the item: {:?}",
                skipped[0].reason
            ),
            other => panic!("expected the item to be held back, got {other:?}"),
        }
    }

    /// The backoff must also EXPIRE, or a transient failure parks an item for
    /// good and the ledger becomes a denylist.
    #[tokio::test]
    async fn a_stale_refusal_becomes_eligible_again_once_its_backoff_passes() {
        let mut claims = ClaimStore::new();
        claims.record_failure("acme/widgets", 5, "transient", 0);
        let (_, f) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            // Past the first backoff step.
            now: super::super::heal_claims::BACKOFF_BASE_MS + 1,
            ..Default::default()
        });
        let out = tick(&f, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(
            !matches!(out, TickOutcome::Idle { .. }),
            "an expired backoff must release the item, got {out:?}"
        );
    }

    /// The other half: an item that VANISHED needs no backoff. It is gone from
    /// the queue, so a later tick will not see it, and recording a failure
    /// would leave a phantom entry against an issue nobody can look at.
    #[tokio::test]
    async fn an_item_that_vanished_is_not_recorded_as_a_failure() {
        struct Vanished;
        #[async_trait::async_trait]
        impl TickIo for Vanished {
            async fn candidates(&self, _t: &HealTarget) -> Result<Vec<Candidate>, String> {
                Ok(vec![item()])
            }
            async fn open_prs(&self, _t: &HealTarget) -> Result<Vec<RawPullRequest>, String> {
                Ok(vec![])
            }
            async fn intent_for(&self, _i: &Candidate) -> Result<Intent, String> {
                Ok(Intent::Gone)
            }
            fn redact(&self, t: &str) -> String {
                t.to_string()
            }
            async fn run_coder(
                &self,
                _t: &HealTarget,
                _i: &Candidate,
                _s: &SessionSeed,
            ) -> Result<Attempt, RunFailure> {
                panic!("a vanished item must not reach the coder")
            }
            async fn deliver(
                &self,
                _t: &HealTarget,
                _i: &Candidate,
                _s: &str,
                _g: &GateOutcome,
            ) -> Result<PrDeliveryOutcome, DeliverRefusal> {
                panic!("a vanished item must not be delivered")
            }
            async fn abandon(&self, _s: &str) {}
            async fn comment(&self, _i: &Candidate, _t: &str) -> Result<(), String> {
                Ok(())
            }
            fn now_ms(&self) -> u64 {
                1_000_000
            }
        }
        let io: Arc<dyn TickIo> = Arc::new(Vanished);
        let mut claims = ClaimStore::new();
        let out = tick(&io, &target(), &mut claims, "run-1", &NoClaimSink).await;
        match out {
            TickOutcome::Idle { skipped } => {
                assert_eq!(skipped[0].reason, super::super::heal_select::Skip::Gone)
            }
            other => panic!("expected idle, got {other:?}"),
        }
        assert!(
            claims.attempts().is_empty(),
            "a vanished item leaves no backoff against an issue nobody can see"
        );
    }

    /// A stale tier is not an authorisation failure, and saying so would send
    /// an operator to check permissions when the remedy is to retry.
    #[tokio::test]
    async fn a_stale_tier_reports_as_not_cleared_not_as_untrusted() {
        struct Stale;
        #[async_trait::async_trait]
        impl TickIo for Stale {
            async fn candidates(&self, _t: &HealTarget) -> Result<Vec<Candidate>, String> {
                Ok(vec![item()])
            }
            async fn open_prs(&self, _t: &HealTarget) -> Result<Vec<RawPullRequest>, String> {
                Ok(vec![])
            }
            async fn intent_for(&self, _i: &Candidate) -> Result<Intent, String> {
                Ok(Intent::Refused {
                    tier: ProvenanceTier::Maintainer,
                    stale: true,
                })
            }
            fn redact(&self, t: &str) -> String {
                t.to_string()
            }
            async fn run_coder(
                &self,
                _t: &HealTarget,
                _i: &Candidate,
                _s: &SessionSeed,
            ) -> Result<Attempt, RunFailure> {
                panic!("unreached")
            }
            async fn deliver(
                &self,
                _t: &HealTarget,
                _i: &Candidate,
                _s: &str,
                _g: &GateOutcome,
            ) -> Result<PrDeliveryOutcome, DeliverRefusal> {
                panic!("unreached")
            }
            async fn abandon(&self, _s: &str) {}
            async fn comment(&self, _i: &Candidate, _t: &str) -> Result<(), String> {
                Ok(())
            }
            fn now_ms(&self) -> u64 {
                1_000_000
            }
        }
        let io: Arc<dyn TickIo> = Arc::new(Stale);
        let mut claims = ClaimStore::new();
        match tick(&io, &target(), &mut claims, "run-1", &NoClaimSink).await {
            TickOutcome::Idle { skipped } => assert_eq!(
                skipped[0].reason,
                super::super::heal_select::Skip::IntentNotCleared
            ),
            other => panic!("expected idle, got {other:?}"),
        }
        // Still recorded: a stale tier that keeps being stale would otherwise
        // halt the sweep on this item exactly as an untrusted author did.
        assert!(!claims.attempts().is_empty());
    }

    /// A coder failure that left a live session must close it out.
    ///
    /// `run` can fail AFTER the session reached `NeedsApproval` — no worktree,
    /// an unreadable diff, a green contract over an unchanged worktree — and a
    /// plain `String` error gave the caller no way to name it. Each one leaked
    /// a git worktree registered in the operator's own repository, with the
    /// task handle already taken so `coder.cancel` could not reach it, and the
    /// reaper that would have swept them was deleted on the strength of the
    /// invariant these paths break.
    #[tokio::test]
    async fn a_failed_coder_run_still_closes_its_session() {
        let (typed, f) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            coder_err: Some("the worktree is unchanged".into()),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&f, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Failed { .. }));
        assert!(
            calls_of(&typed).contains(&"abandon".to_string()),
            "the session was left non-terminal, holding a worktree: {:?}",
            calls_of(&typed)
        );
    }

    /// Exhausting reviewer exclusions is a daemon configuration failure, not
    /// evidence that the selected backlog item is bad. The tick still closes
    /// the session and releases its claim, but leaves neither item backoff nor
    /// a public failure comment.
    #[tokio::test]
    async fn no_independent_coder_does_not_penalize_or_comment_on_the_item() {
        let (typed, f) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            coder_err: Some("no independent coder model is available; configure heal.toml".into()),
            coder_configuration_error: true,
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&f, &target(), &mut claims, "run-1", &NoClaimSink).await;

        assert!(matches!(out, TickOutcome::Failed { .. }));
        assert!(claims.attempts().is_empty(), "item gained failure backoff");
        assert_eq!(claims.held_by("acme/widgets", 5), None);
        assert_eq!(
            calls_of(&typed),
            vec!["candidates", "intent", "coder", "abandon"],
            "configuration failure must not post on the backlog item"
        );
    }

    #[tokio::test]
    async fn an_empty_queue_is_idle_not_failure() {
        let (_, f) = io(Fake {
            intent: Some("fix it".into()),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&f, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Idle { .. }));
    }

    #[tokio::test]
    async fn a_watch_only_target_spends_no_api_call() {
        let (typed, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            ..Default::default()
        });
        let mut t = target();
        t.checkout = None;
        let out = tick(&arc, &t, &mut claims_new(), "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Idle { .. }));
        // Reading a queue we can do nothing about is pure cost.
        assert!(calls_of(&typed).is_empty());
    }

    fn claims_new() -> ClaimStore {
        ClaimStore::new()
    }

    #[tokio::test]
    async fn an_invalid_repo_spec_fails_rather_than_idling() {
        let (_, f) = io(Fake::default());
        let mut t = target();
        t.repo = "not-a-spec".into();
        let out = tick(&f, &t, &mut claims_new(), "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Failed { .. }));
    }

    #[tokio::test]
    async fn unavailable_ci_keeps_delivery_claim_without_failure_backoff() {
        let (typed, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix parser".into()),
            ci_unavailable: true,
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(
            matches!(out, TickOutcome::Opened { ref delivery, .. } if delivery.contains("CI unavailable"))
        );
        assert_eq!(claims.held_by("acme/widgets", 5), Some("run-1"));
        assert!(claims.attempts().is_empty());
        assert!(!calls_of(&typed).contains(&"abandon".to_string()));
        let again = tick(&arc, &target(), &mut claims, "run-2", &NoClaimSink).await;
        assert!(matches!(again, TickOutcome::Idle { .. }));
        assert_eq!(
            calls_of(&typed)
                .iter()
                .filter(|c| c.as_str() == "deliver")
                .count(),
            1
        );
    }

    #[tokio::test]
    async fn a_happy_tick_claims_then_works_then_opens() {
        let (typed, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix the parser".into()),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        match out {
            TickOutcome::Opened {
                number,
                pr_url,
                ci,
                delivery,
                ..
            } => {
                assert_eq!(number, 5);
                assert!(pr_url.contains("pr/1"));
                assert_eq!(ci.head_sha, "head123");
                assert_eq!(ci.state, super::super::merge::CiState::Green);
                assert_eq!(
                    delivery,
                    "delivered with green checks and ready for review at head123"
                );
            }
            other => panic!("expected Opened, got {other:?}"),
        }
        // The claim is HELD after success: the item is not free until a human
        // closes it, and a later tick must not immediately redo the work.
        assert_eq!(claims.held_by("acme/widgets", 5), Some("run-1"));

        let calls = calls_of(&typed);
        let claim_before_work = calls.iter().position(|c| c == "coder").unwrap();
        assert!(
            calls[..claim_before_work].contains(&"intent".to_string()),
            "intent is cleared before the coder runs: {calls:?}"
        );
    }

    #[tokio::test]
    async fn an_uncleared_intent_releases_the_claim() {
        // `None` means the item could not be trust-cleared. It must never fall
        // back to the raw body, and it must not leave the item claimed.
        let (typed, arc) = io(Fake {
            candidates: vec![item()],
            intent: None,
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Idle { .. }));
        assert_eq!(claims.held_by("acme/widgets", 5), None, "claim released");

        assert!(
            !calls_of(&typed).contains(&"coder".to_string()),
            "the coder must not run on uncleared intent"
        );
    }

    #[tokio::test]
    async fn a_rejected_gate_comments_and_releases() {
        let (typed, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            attempt: Some(Attempt {
                contract_passed: true,
                contract_detail: "green".into(),
                panel_size: 3,
                verdicts: vec![
                    Verdict {
                        model: "a".into(),
                        pass: true,
                        reason: "ok".into(),
                    },
                    Verdict {
                        model: "b".into(),
                        pass: false,
                        reason: "changes unrelated behaviour".into(),
                    },
                    Verdict {
                        model: "c".into(),
                        pass: false,
                        reason: "scope".into(),
                    },
                ],
                unreachable: vec![],
                session_id: "coder-e2e-1".into(),
            }),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Rejected { .. }));
        assert_eq!(claims.held_by("acme/widgets", 5), None);

        let calls = calls_of(&typed);
        assert!(
            calls.contains(&"comment".to_string()),
            "must explain itself"
        );
        assert!(
            !calls.contains(&"deliver".to_string()),
            "a rejected change must not be opened"
        );
    }

    #[tokio::test]
    async fn a_failed_coder_releases_the_claim() {
        let (_, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            coder_err: Some("worktree gone".into()),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Failed { .. }));
        assert_eq!(claims.held_by("acme/widgets", 5), None);
    }

    #[tokio::test]
    async fn a_failed_pr_open_releases_so_a_later_tick_can_retry() {
        let (_, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            pr_err: Some("gh auth expired".into()),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Failed { .. }));
        assert_eq!(
            claims.held_by("acme/widgets", 5),
            None,
            "otherwise the item sits claimed and invisible"
        );
    }

    #[tokio::test]
    async fn an_item_claimed_by_another_run_is_left_alone() {
        let (typed, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        claims
            .claim("acme/widgets", 5, "other-run", 10 * CLAIM_TTL_MS)
            .unwrap();
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Idle { .. }));
        assert_eq!(claims.held_by("acme/widgets", 5), Some("other-run"));

        assert!(!calls_of(&typed).contains(&"coder".to_string()));
    }

    #[tokio::test]
    async fn an_expired_claim_lets_a_later_tick_pick_it_up() {
        let (_, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        // The fake's clock is 10x the TTL, so a claim at time 0 is long dead.
        claims.claim("acme/widgets", 5, "dead-run", 0).unwrap();
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Opened { .. }));
    }

    #[tokio::test]
    async fn an_issue_already_covered_is_not_worked_twice() {
        let (_, arc) = io(Fake {
            candidates: vec![item()],
            prs: vec![RawPullRequest::new(
                "acme/widgets",
                9,
                "someone",
                "fix",
                "closes #5",
                vec![],
                ChecksState::Passing,
                "",
                false,
            )],
            intent: Some("fix it".into()),
            ..Default::default()
        });
        let out = tick(&arc, &target(), &mut claims_new(), "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Idle { .. }));
    }

    #[test]
    fn a_project_target_and_a_local_target_map_to_different_coder_arguments() {
        let (repo, project) = coder_target(&target()).unwrap();
        assert!(repo.is_none() && project.as_deref() == Some("widgets"));

        let mut t = target();
        t.checkout = Some(Checkout::Local("/tmp/x".into()));
        let (repo, project) = coder_target(&t).unwrap();
        assert!(repo.is_some() && project.is_none());
    }

    #[tokio::test]
    async fn a_rejected_item_is_not_retried_on_the_very_next_tick() {
        // THE bug both reviews ranked first. Oldest-first selection plus
        // release-on-failure plus no memory = a metronome pointed at one issue,
        // burning a coder session and a model panel every tick while everything
        // behind it starves.
        let (_, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            attempt: Some(Attempt {
                contract_passed: true,
                contract_detail: "green".into(),
                panel_size: 3,
                verdicts: vec![
                    Verdict {
                        model: "a".into(),
                        pass: false,
                        reason: "no".into(),
                    },
                    Verdict {
                        model: "b".into(),
                        pass: false,
                        reason: "no".into(),
                    },
                ],
                unreachable: vec![],
                session_id: "coder-b".into(),
            }),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        let first = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(first, TickOutcome::Rejected { .. }));
        assert_eq!(claims.attempts().len(), 1, "the failure is remembered");

        // The claim was released, so only the backoff can stop a re-pick.
        let a = &claims.attempts()[&crate::coder::heal_select::claim_key("acme/widgets", 5)];
        assert!(!a.ready(arc.now_ms()), "not eligible again immediately");
    }

    #[tokio::test]
    async fn a_caller_cannot_mint_approval_it_did_not_earn() {
        // `run_coder` returns evidence; only `decide` builds a verdict. A
        // failing contract cannot be reported as approved no matter what the
        // panel says.
        let (_, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            attempt: Some(Attempt {
                contract_passed: false,
                contract_detail: "cargo test failed".into(),
                panel_size: 3,
                verdicts: vec![
                    Verdict {
                        model: "a".into(),
                        pass: true,
                        reason: "lgtm".into(),
                    },
                    Verdict {
                        model: "b".into(),
                        pass: true,
                        reason: "lgtm".into(),
                    },
                    Verdict {
                        model: "c".into(),
                        pass: true,
                        reason: "lgtm".into(),
                    },
                ],
                unreachable: vec![],
                session_id: "coder-b".into(),
            }),
            ..Default::default()
        });
        let out = tick(
            &arc,
            &target(),
            &mut ClaimStore::new(),
            "run-1",
            &NoClaimSink,
        )
        .await;
        match out {
            TickOutcome::Rejected { gate, .. } => {
                assert!(gate.contains("contract"), "{gate}");
            }
            other => panic!("a red contract must not open a PR: {other:?}"),
        }
    }

    #[tokio::test]
    async fn a_success_clears_the_failure_history() {
        let (_, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            ..Default::default()
        });
        let mut claims = ClaimStore::new();
        claims.record_failure("acme/widgets", 5, "earlier", 0);
        let out = tick(&arc, &target(), &mut claims, "run-1", &NoClaimSink).await;
        assert!(matches!(out, TickOutcome::Opened { .. }));
        assert!(claims.attempts().is_empty(), "a later failure starts clean");
    }

    #[tokio::test]
    async fn every_stop_says_why_on_the_item() {
        // A gate that passed but whose PR could not be opened is the case a
        // human most needs told, and it used to return silently.
        let (typed, arc) = io(Fake {
            candidates: vec![item()],
            intent: Some("fix it".into()),
            pr_err: Some("gh auth expired".into()),
            ..Default::default()
        });
        let out = tick(
            &arc,
            &target(),
            &mut ClaimStore::new(),
            "run-1",
            &NoClaimSink,
        )
        .await;
        assert!(matches!(out, TickOutcome::Failed { .. }));
        assert!(
            calls_of(&typed).contains(&"comment".to_string()),
            "a silent failure teaches people to ignore the loop"
        );
    }

    #[tokio::test]
    async fn a_watch_only_target_reports_why_it_did_nothing() {
        let (_, arc) = io(Fake::default());
        let mut t = target();
        t.checkout = None;
        match tick(&arc, &t, &mut ClaimStore::new(), "run-1", &NoClaimSink).await {
            TickOutcome::Idle { skipped } => {
                assert_eq!(skipped.len(), 1);
                assert_eq!(
                    skipped[0].reason,
                    crate::coder::heal_select::Skip::WatchOnly
                );
            }
            other => panic!("expected an explained idle, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn an_uncleared_intent_reports_clearance_not_authorisation() {
        // Reporting "untrusted author: maintainer" sends an operator to check
        // permissions when the remedy is to retry.
        let (_, arc) = io(Fake {
            candidates: vec![item()],
            intent: None,
            intent_stale: true,
            ..Default::default()
        });
        match tick(
            &arc,
            &target(),
            &mut ClaimStore::new(),
            "run-1",
            &NoClaimSink,
        )
        .await
        {
            TickOutcome::Idle { skipped } => {
                assert_eq!(
                    skipped[0].reason,
                    crate::coder::heal_select::Skip::IntentNotCleared
                );
            }
            other => panic!("expected an explained idle, got {other:?}"),
        }
    }
}