kranz-engine 0.2.2

Governed mission engine for auditable AI coding-agent work.
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
//! Host-callable queue drain core (roadmap f-1-1): the drain/claim/skip loop
//! that used to live only inside `kranz` CLI's `cmd_work`, hoisted here so any
//! surface (CLI, REST, Slack) can drain a repo's queue.
//!
//! [`drain_queue`] owns the loop — recover dead claims, then peek/claim/run
//! one mission at a time — but does NOT run missions itself: the caller
//! injects a `run_mission` closure, because each host supplies its own runner
//! (the CLI tails events to stderr; a headless caller does not) and only the
//! caller knows how to restore its own git checkout.

use crate::backend_readiness::{self, DrainDecision};
use crate::deps;
use crate::event_log::EventLog;
use crate::paths::MissionPaths;
use crate::queue::{self, QueueEntry};
use crate::reducer;
use crate::ticket::{Ticket, TicketState};
use crate::types::MissionStatus;
use anyhow::Result;
use std::future::Future;
use std::path::Path;

// ---------------------------------------------------------------------------
// work dispatcher — pure decision helper
// ---------------------------------------------------------------------------

/// The dispatcher's next action given the queue front and repo-busy state.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WorkAction {
    /// Nothing queued: the dispatcher exits.
    Empty,
    /// The repo is busy running `mission_id`: wait (default) or exit (`--once`).
    Busy { mission_id: String },
    /// Free to run the front mission.
    Run {
        mission_id: String,
        ticket_slug: Option<String>,
    },
}

/// Decide the dispatcher's next step from the queue front + busy state.
/// Pure: `front` is `queue::peek`, `busy_with` is `queue::is_repo_busy`.
pub fn next_work_action(front: Option<&QueueEntry>, busy_with: Option<&str>) -> WorkAction {
    match front {
        None => WorkAction::Empty,
        Some(_) if busy_with.is_some() => WorkAction::Busy {
            mission_id: busy_with.expect("checked is_some").to_string(),
        },
        Some(entry) => WorkAction::Run {
            mission_id: entry.mission_id.clone(),
            ticket_slug: entry.ticket_slug.clone(),
        },
    }
}

/// Work-time re-check for a claimed queue entry with a ticket: `Some(blocker)`
/// when one of the ticket's unsatisfied `blocked-by` entries is unsatisfied
/// because that blocker's own ticket ended up Failed (its mission reached a
/// terminal non-Complete state — Failed/Abandoned/Blocked — after
/// batch-approval queued this entry alongside it). The dispatcher must skip
/// such an entry rather than run it: re-driving a mission whose dependency
/// failed can never succeed, and retrying forever would hot-loop.
pub fn work_skip_for_failed_blocker(repo_root: &Path, slug: &str) -> Result<Option<String>> {
    let unsatisfied = deps::unsatisfied_blockers(repo_root, slug)?;
    for blocker in unsatisfied {
        if Ticket::read_state(repo_root, &blocker) == TicketState::Failed {
            return Ok(Some(blocker));
        }
    }
    Ok(None)
}

/// Map a terminal (or blocked) mission status to the ticket state recorded
/// after a run. Only called by [`reconcile_ticket_for_mission`] for
/// terminal/blocked statuses — live statuses are gated out before this runs.
pub fn ticket_state_for_mission(status: MissionStatus) -> TicketState {
    match status {
        MissionStatus::Complete => TicketState::Done,
        MissionStatus::Failed => TicketState::Failed,
        MissionStatus::Abandoned => TicketState::Failed,
        // Blocked is needs-input, not a failure: the mission is waiting on a
        // human, so the ticket should resurface as NeedsContext, not Failed.
        MissionStatus::Blocked => TicketState::NeedsContext,
        _ => TicketState::Failed,
    }
}

/// The single authoritative reconcile helper: given a mission id, reverse-
/// looks-up its linked ticket and, if the mission's folded status is
/// terminal-or-blocked, writes the mapped [`TicketState`] to the ticket's
/// `.status` sidecar. LIVE statuses (Running/Validating/Paused/Approved/
/// Planning) are a no-op — the ticket is still mid-flight and must not be
/// clobbered. Every path that can drive a mission to a terminal (or blocked)
/// state — `kranz run`, `kranz exec`, REST `/start`, the drain loop — should
/// call this instead of writing the ticket state itself, so the stale-
/// "Failed" heal case and the Blocked-to-NeedsContext mapping live in one
/// place.
///
/// Defensive by design (mirrors [`crate::merged::ticket_merged`]): an
/// unlinked or unloadable mission is `Ok(None)`, never an error — reconcile
/// must never fail the caller's terminal-state transition.
pub fn reconcile_ticket_for_mission(
    repo_root: &Path,
    mission_id: &str,
) -> crate::error::Result<Option<(String, TicketState)>> {
    let Some(slug) = Ticket::slug_for_mission(repo_root, mission_id) else {
        return Ok(None);
    };

    let paths = MissionPaths::new(repo_root, mission_id);
    if !paths.events_file().is_file() {
        return Ok(None);
    }
    let Ok(events) = EventLog::read_events(&paths.events_file()) else {
        return Ok(None);
    };
    let Ok(state) = reducer::fold(&events) else {
        return Ok(None);
    };

    let status = state.mission.status;
    if !matches!(
        status,
        MissionStatus::Complete
            | MissionStatus::Failed
            | MissionStatus::Abandoned
            | MissionStatus::Blocked
    ) {
        return Ok(None);
    }

    let mapped = ticket_state_for_mission(status);
    let current = Ticket::read_state(repo_root, &slug);
    if current == mapped {
        return Ok(None);
    }
    Ticket::write_state(repo_root, &slug, mapped, None)?;
    Ok(Some((slug, mapped)))
}

// ---------------------------------------------------------------------------
// drain_queue — the shared drain/claim/skip loop
// ---------------------------------------------------------------------------

/// Outcome of a [`drain_queue`] call: the mission ids that ran to a terminal
/// state vs. those skipped because a blocker failed.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DrainReport {
    pub ran: Vec<String>,
    pub skipped: Vec<String>,
    /// Claimed then parked because backend readiness failed hard (ticket
    /// marked [`TicketState::Parked`] with a readiness note when linked).
    pub parked: Vec<String>,
    /// `once` stopped the drain while the repo was busy with another mission
    /// (nothing here was claimed or run). Callers that restore their own git
    /// checkout on exit must NOT do so when this is set — the repo is still
    /// mid-mission under a sibling dispatcher, and switching branches out
    /// from under it would corrupt that run's working tree.
    pub stopped_busy: bool,
    /// An expectation-guarded one-shot drain found a different mission at
    /// the front. The claim was released without readiness probing or run.
    pub expected_mismatch: Option<String>,
}

/// After this many consecutive rate-limit delays on the same mission id in one
/// drain, park instead of starving the rest of the queue forever.
const RATE_LIMIT_ROTATE_CAP: u32 = 3;

/// Drain the per-repo queue one mission at a time. Recover dead claims once up
/// front; then atomically claim the front entry with the repo-wide busy guard;
/// on `Busy` either return (`once`) or sleep 5s and retry; on a lost claim race,
/// retry after a brief sleep; on `Claimed`, run backend readiness under the
/// claim (park → finish_claim; rate-limit → release + rotate/delay; ok → run),
/// skip a ticket-born entry whose blocker failed, otherwise write ticket
/// Running, invoke the injected `run_mission`, then finish/release the claim,
/// write the terminal ticket state, and honor `once`.
///
/// Readiness is probed **after** claim so a sibling drain cannot race a peek
/// + `queue::remove` into marking a live run's ticket Failed.
///
/// Does NOT do checkout restoration or event printing — those stay with the
/// caller, which is exactly why `run_mission` is injected rather than run
/// inside this core: the CLI keeps its live event tail, while a headless
/// caller (REST, Slack) can drive the same loop with no terminal attached.
pub async fn drain_queue<R, Fut>(
    repo_root: &Path,
    once: bool,
    run_mission: R,
) -> Result<DrainReport>
where
    R: Fn(String) -> Fut,
    Fut: Future<Output = Result<i32>>,
{
    drain_queue_expected(repo_root, once, None, run_mission).await
}

/// Drain with an optional atomic front-entry expectation. When `expected` is
/// set and a sibling dispatcher changed the front before the claim landed,
/// the claimed entry is released and returned in
/// [`DrainReport::expected_mismatch`] without running it. This is the
/// supervised-adapter guard for producers that must translate one specific
/// queued mission's outcome back to an external system.
pub async fn drain_queue_expected<R, Fut>(
    repo_root: &Path,
    once: bool,
    expected: Option<&str>,
    run_mission: R,
) -> Result<DrainReport>
where
    R: Fn(String) -> Fut,
    Fut: Future<Output = Result<i32>>,
{
    drain_queue_with_probe_expected(
        repo_root,
        once,
        expected,
        run_mission,
        backend_readiness::probe_mission,
    )
    .await
}

/// [`drain_queue`] with an injectable readiness probe. Engine and host tests
/// use this seam to script proceed / park / rate-limit decisions without
/// shelling out to whichever agent CLIs happen to be installed on the test
/// machine.
pub async fn drain_queue_with_probe<R, Fut, P>(
    repo_root: &Path,
    once: bool,
    run_mission: R,
    probe: P,
) -> Result<DrainReport>
where
    R: Fn(String) -> Fut,
    Fut: Future<Output = Result<i32>>,
    P: Fn(&Path, &str) -> crate::error::Result<backend_readiness::ReadinessReport>,
{
    drain_queue_with_probe_expected(repo_root, once, None, run_mission, probe).await
}

async fn drain_queue_with_probe_expected<R, Fut, P>(
    repo_root: &Path,
    once: bool,
    expected: Option<&str>,
    run_mission: R,
    probe: P,
) -> Result<DrainReport>
where
    R: Fn(String) -> Fut,
    Fut: Future<Output = Result<i32>>,
    P: Fn(&Path, &str) -> crate::error::Result<backend_readiness::ReadinessReport>,
{
    let mut report = DrainReport::default();
    let mut rate_limit_hits: std::collections::HashMap<String, u32> =
        std::collections::HashMap::new();
    queue::recover_dead_claims(repo_root);
    loop {
        match queue::claim_front_when_repo_free(repo_root)? {
            queue::ClaimFront::Empty => return Ok(report),
            queue::ClaimFront::LostRace => {
                tokio::time::sleep(std::time::Duration::from_millis(250)).await;
                continue;
            }
            queue::ClaimFront::Busy { .. } => {
                if once {
                    report.stopped_busy = true;
                    return Ok(report);
                }
                tokio::time::sleep(std::time::Duration::from_secs(5)).await;
                continue;
            }
            queue::ClaimFront::Claimed(claim) => {
                let mission_id = claim.entry.mission_id.clone();
                if expected.is_some_and(|expected| expected != mission_id) {
                    queue::release_claim(claim);
                    report.expected_mismatch = Some(mission_id);
                    return Ok(report);
                }
                let ticket_slug = claim
                    .entry
                    .ticket_slug
                    .clone()
                    .or_else(|| Ticket::slug_for_mission(repo_root, &mission_id));

                // Backend readiness under the claim (no peek/remove race).
                match probe(repo_root, &mission_id) {
                    Ok(readiness) => match readiness.drain_decision() {
                        DrainDecision::Proceed { warnings } => {
                            for w in warnings {
                                tracing::warn!(
                                    mission = %mission_id,
                                    warning = %w,
                                    "backend readiness warning; proceeding"
                                );
                            }
                        }
                        DrainDecision::Park { reason } => {
                            tracing::warn!(
                                mission = %mission_id,
                                reason = %reason,
                                "parking claimed mission: backend not ready"
                            );
                            let stop = settle_parked_claim(
                                repo_root,
                                claim,
                                ticket_slug.as_deref(),
                                format!("parked (backend not ready): {reason}"),
                            )?;
                            report.parked.push(mission_id);
                            if once || stop {
                                return Ok(report);
                            }
                            continue;
                        }
                        DrainDecision::RequeueDelay { reason, delay } => {
                            let hits = rate_limit_hits.entry(mission_id.clone()).or_insert(0);
                            *hits += 1;
                            let hits = *hits;
                            tracing::warn!(
                                mission = %mission_id,
                                reason = %reason,
                                delay_secs = delay.as_secs(),
                                hits,
                                "backend rate-limited after claim"
                            );
                            if hits >= RATE_LIMIT_ROTATE_CAP {
                                let stop = settle_parked_claim(
                                    repo_root,
                                    claim,
                                    ticket_slug.as_deref(),
                                    format!("parked (rate-limited {hits}×): {reason}"),
                                )?;
                                report.parked.push(mission_id);
                                if once || stop {
                                    return Ok(report);
                                }
                                continue;
                            }
                            // Release back to the queue, then rotate behind
                            // other same-priority work so one limited head
                            // cannot starve the drain forever.
                            let entry = claim.entry.clone();
                            queue::release_claim(claim);
                            rotate_entry_to_back(repo_root, &entry)?;
                            if once {
                                return Ok(report);
                            }
                            // Production delay is typically 60s; tests clamp so
                            // rate-limit rotate/park coverage stays hermetic.
                            #[cfg(test)]
                            let delay = delay.min(std::time::Duration::from_millis(1));
                            tokio::time::sleep(delay).await;
                            continue;
                        }
                    },
                    Err(e) => {
                        tracing::warn!(
                            mission = %mission_id,
                            error = %e,
                            "backend readiness probe failed; proceeding"
                        );
                    }
                }

                // Disk-footprint preflight (ticket mission-build-footprint):
                // refuse to START a mission whose build ladder won't fit in
                // the free space under the repo, naming the estimate, rather
                // than dying mid-feature with os error 28. Unmeasurable free
                // space degrades to proceed (never a fabricated refusal).
                if let crate::disk_preflight::DiskPreflight::Insufficient {
                    free_bytes,
                    estimate_bytes,
                } = crate::disk_preflight::check(repo_root)
                {
                    let reason = format!(
                        "insufficient disk for the mission build ladder: {} free < {} estimated \
                         (free space or raise the estimate)",
                        crate::disk_preflight::gib(free_bytes),
                        crate::disk_preflight::gib(estimate_bytes)
                    );
                    tracing::warn!(mission = %mission_id, reason = %reason, "parking claimed mission: disk preflight");
                    let stop = settle_parked_claim(
                        repo_root,
                        claim,
                        ticket_slug.as_deref(),
                        format!("parked (disk): {reason}"),
                    )?;
                    report.parked.push(mission_id);
                    if once || stop {
                        return Ok(report);
                    }
                    continue;
                }

                if let Some(slug) = &ticket_slug {
                    if let Some(blocker) = work_skip_for_failed_blocker(repo_root, slug)? {
                        queue::finish_claim(claim);
                        Ticket::write_state(
                            repo_root,
                            slug,
                            TicketState::Failed,
                            Some(format!("skipped: blocked-by {blocker} failed")),
                        )?;
                        report.skipped.push(mission_id);
                        continue;
                    }
                    Ticket::write_state(repo_root, slug, TicketState::Running, None)?;
                }

                let status = run_mission(mission_id.clone()).await;

                match &status {
                    Ok(_) => queue::finish_claim(claim),
                    Err(_) if ticket_slug.is_some() => queue::finish_claim(claim),
                    Err(_) => queue::release_claim(claim),
                }

                if ticket_slug.is_some() {
                    reconcile_ticket_for_mission(repo_root, &mission_id)?;
                } else {
                    status?;
                }
                report.ran.push(mission_id);

                if once {
                    return Ok(report);
                }
            }
        }
    }
}

/// Ticket-backed work has a durable Parked projection, so its queue entry can
/// retire. A raw `exec --enqueue` mission has no ticket projection: releasing
/// its claim is the only durable not-yet-run state. Stop this drain after the
/// release so a persistent preflight failure cannot hot-loop on the same head.
fn settle_parked_claim(
    repo_root: &Path,
    claim: queue::Claim,
    ticket_slug: Option<&str>,
    note: String,
) -> Result<bool> {
    if let Some(slug) = ticket_slug {
        queue::finish_claim(claim);
        Ticket::write_state(repo_root, slug, TicketState::Parked, Some(note))?;
        Ok(false)
    } else {
        queue::release_claim(claim);
        Ok(true)
    }
}

/// Drop `entry` from the queue (if still present) and re-enqueue so it gets a
/// fresh seq and sorts behind other same-priority work.
fn rotate_entry_to_back(repo_root: &Path, entry: &QueueEntry) -> Result<()> {
    queue::remove(repo_root, &entry.mission_id);
    let _ = queue::enqueue(
        repo_root,
        QueueEntry {
            mission_id: entry.mission_id.clone(),
            ticket_slug: entry.ticket_slug.clone(),
            priority: entry.priority,
            // seq overwritten by enqueue
            seq: 0,
        },
    )?;
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::events::{Event, EventKind};
    use crate::types::MissionConfig;
    use chrono::Utc;
    use std::sync::atomic::{AtomicUsize, Ordering};
    use std::sync::Arc;

    #[test]
    fn next_work_action_empty_when_no_front() {
        assert_eq!(next_work_action(None, None), WorkAction::Empty);
    }

    #[test]
    fn next_work_action_busy_takes_priority_over_front() {
        let entry = QueueEntry {
            mission_id: "m1".to_string(),
            ticket_slug: None,
            priority: 2,
            seq: 0,
        };
        assert_eq!(
            next_work_action(Some(&entry), Some("busy-mission")),
            WorkAction::Busy {
                mission_id: "busy-mission".to_string()
            }
        );
    }

    #[test]
    fn next_work_action_runs_the_front_when_idle() {
        let entry = QueueEntry {
            mission_id: "m1".to_string(),
            ticket_slug: Some("slug-a".to_string()),
            priority: 2,
            seq: 0,
        };
        assert_eq!(
            next_work_action(Some(&entry), None),
            WorkAction::Run {
                mission_id: "m1".to_string(),
                ticket_slug: Some("slug-a".to_string()),
            }
        );
    }

    #[test]
    fn ticket_state_for_mission_maps_terminal_status() {
        assert_eq!(
            ticket_state_for_mission(MissionStatus::Complete),
            TicketState::Done
        );
        assert_eq!(
            ticket_state_for_mission(MissionStatus::Blocked),
            TicketState::NeedsContext
        );
        assert_eq!(
            ticket_state_for_mission(MissionStatus::Failed),
            TicketState::Failed
        );
        assert_eq!(
            ticket_state_for_mission(MissionStatus::Abandoned),
            TicketState::Failed
        );
    }

    fn write_ticket(repo: &Path, slug: &str, body: &str) {
        let dir = Ticket::tickets_dir(repo);
        std::fs::create_dir_all(&dir).unwrap();
        std::fs::write(dir.join(format!("{slug}.md")), body).unwrap();
    }

    /// Write a hand-built events.jsonl for `mission_id` under `repo_root`
    /// (mirrors `merged_test.rs::write_events`).
    fn write_events(repo_root: &Path, mission_id: &str, kinds: Vec<EventKind>) {
        let dir = repo_root.join(".kranz").join("missions").join(mission_id);
        std::fs::create_dir_all(&dir).unwrap();
        let mut lines = String::new();
        for (i, kind) in kinds.into_iter().enumerate() {
            let event = Event {
                seq: (i + 1) as u64,
                ts: Utc::now(),
                mission_id: mission_id.to_string(),
                kind,
            };
            lines.push_str(&serde_json::to_string(&event).unwrap());
            lines.push('\n');
        }
        std::fs::write(dir.join("events.jsonl"), lines).unwrap();
    }

    fn created() -> EventKind {
        EventKind::MissionCreated {
            goal: "fixture mission".to_string(),
            base_branch: "main".to_string(),
            mission_branch: "kranz/mission-fixture".to_string(),
            config: MissionConfig::default(),
        }
    }

    fn scaffold_ticket(repo_root: &Path, slug: &str, mission_id: &str, state: TicketState) {
        Ticket::scaffold(repo_root, slug, "fixture ticket", None, None).unwrap();
        Ticket::record_mission(repo_root, slug, mission_id).unwrap();
        Ticket::write_state(repo_root, slug, state, None).unwrap();
    }

    fn plan_with_one_milestone() -> crate::types::Plan {
        crate::types::Plan {
            goal: "fixture goal".to_string(),
            validation_contract: vec![],
            milestones: vec![crate::types::PlanMilestone {
                title: "milestone one".to_string(),
                features: vec![crate::types::PlanFeature {
                    title: "feature one".to_string(),
                    spec: "spec".to_string(),
                    validation_criteria: vec!["works".to_string()],
                }],
            }],
            considered_alternatives: None,
            command_grants: vec![],
            touch_set: vec![],
            standards_manifest: None,
            reviewer_independence: None,
        }
    }

    #[test]
    fn reconcile_on_terminal_maps_complete_to_done() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        scaffold_ticket(repo, "my-ticket", "m1", TicketState::Running);
        write_events(repo, "m1", vec![created(), EventKind::MissionCompleted {}]);

        let result = reconcile_ticket_for_mission(repo, "m1").unwrap();
        assert_eq!(result, Some(("my-ticket".to_string(), TicketState::Done)));
        assert_eq!(Ticket::read_state(repo, "my-ticket"), TicketState::Done);
    }

    #[test]
    fn reconcile_heals_failed_to_done() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        scaffold_ticket(repo, "my-ticket", "m1", TicketState::Failed);
        write_events(repo, "m1", vec![created(), EventKind::MissionCompleted {}]);

        let result = reconcile_ticket_for_mission(repo, "m1").unwrap();
        assert_eq!(result, Some(("my-ticket".to_string(), TicketState::Done)));
        assert_eq!(Ticket::read_state(repo, "my-ticket"), TicketState::Done);
    }

    #[test]
    fn blocked_reconciles_to_needs_you() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        scaffold_ticket(repo, "my-ticket", "m1", TicketState::Running);
        write_events(
            repo,
            "m1",
            vec![
                created(),
                EventKind::PlanApproved {
                    plan: plan_with_one_milestone(),
                    base_sha: None,
                },
                EventKind::MilestoneBlocked {
                    block_context: None,
                    milestone_id: "ms-1".to_string(),
                    reason: "needs input".to_string(),
                },
            ],
        );

        let result = reconcile_ticket_for_mission(repo, "m1").unwrap();
        assert_eq!(
            result,
            Some(("my-ticket".to_string(), TicketState::NeedsContext))
        );
        assert_eq!(
            Ticket::read_state(repo, "my-ticket"),
            TicketState::NeedsContext
        );
    }

    #[test]
    fn reconcile_returns_none_when_no_linked_ticket() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        write_events(repo, "m1", vec![created(), EventKind::MissionCompleted {}]);

        assert_eq!(reconcile_ticket_for_mission(repo, "m1").unwrap(), None);
    }

    #[test]
    fn reconcile_returns_none_when_mission_status_is_live() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        scaffold_ticket(repo, "my-ticket", "m1", TicketState::Running);
        write_events(repo, "m1", vec![created()]);

        assert_eq!(reconcile_ticket_for_mission(repo, "m1").unwrap(), None);
        assert_eq!(Ticket::read_state(repo, "my-ticket"), TicketState::Running);
    }

    fn proceed_report(mission_id: &str) -> backend_readiness::ReadinessReport {
        backend_readiness::ReadinessReport {
            mission_id: mission_id.to_string(),
            roles: vec![],
            overall: backend_readiness::ReadinessStatus::Ok,
            warnings: vec![],
        }
    }

    fn always_proceed(
        _repo: &Path,
        id: &str,
    ) -> crate::error::Result<backend_readiness::ReadinessReport> {
        Ok(proceed_report(id))
    }

    fn park_report(mission_id: &str) -> backend_readiness::ReadinessReport {
        backend_readiness::ReadinessReport {
            mission_id: mission_id.to_string(),
            roles: vec![backend_readiness::RoleReadiness {
                role: "worker".into(),
                backend: "claude".into(),
                status: backend_readiness::ReadinessStatus::Missing,
                detail: "no binary".into(),
                next_action: "install".into(),
            }],
            overall: backend_readiness::ReadinessStatus::Missing,
            warnings: vec![],
        }
    }

    fn rate_limited_report(mission_id: &str) -> backend_readiness::ReadinessReport {
        backend_readiness::ReadinessReport {
            mission_id: mission_id.to_string(),
            roles: vec![backend_readiness::RoleReadiness {
                role: "orchestrator".into(),
                backend: "claude".into(),
                status: backend_readiness::ReadinessStatus::RateLimited,
                detail: "429".into(),
                next_action: "wait".into(),
            }],
            overall: backend_readiness::ReadinessStatus::RateLimited,
            warnings: vec![],
        }
    }

    #[tokio::test]
    async fn drain_queue_runs_a_queued_mission_and_retires_its_claim() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "mission-1".to_string(),
                ticket_slug: None,
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();

        let ran = Arc::new(AtomicUsize::new(0));
        let ran_clone = ran.clone();
        let report = drain_queue_with_probe(
            repo,
            false,
            move |mission_id| {
                let ran = ran_clone.clone();
                async move {
                    assert_eq!(mission_id, "mission-1");
                    ran.fetch_add(1, Ordering::SeqCst);
                    Ok(0)
                }
            },
            always_proceed,
        )
        .await
        .unwrap();

        assert_eq!(ran.load(Ordering::SeqCst), 1);
        assert_eq!(report.ran, vec!["mission-1".to_string()]);
        assert!(report.skipped.is_empty());
        // The claim file was retired: nothing left on disk under the queue dir.
        assert!(queue::list(repo).is_empty());
        let dir = queue::queue_dir(repo);
        let leftover: Vec<_> = std::fs::read_dir(&dir)
            .unwrap()
            .flatten()
            .filter(|e| {
                e.path()
                    .file_name()
                    .and_then(|n| n.to_str())
                    .is_some_and(|n| n.contains(".claimed."))
            })
            .collect();
        assert!(leftover.is_empty(), "claim file was not retired");
    }

    #[tokio::test]
    async fn drain_queue_skips_ticket_whose_blocker_failed() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();

        write_ticket(
            repo,
            "blocker",
            "---\ntitle: blocker\npriority: 2\nschedule: once\n---\n\n## Goal\nblock\n",
        );
        write_ticket(
            repo,
            "dependent",
            "---\ntitle: dependent\npriority: 2\nschedule: once\nblocked-by: [blocker]\n---\n\n## Goal\ndepend\n",
        );
        Ticket::write_state(repo, "blocker", TicketState::Failed, None).unwrap();

        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "mission-dep".to_string(),
                ticket_slug: Some("dependent".to_string()),
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();

        let ran = Arc::new(AtomicUsize::new(0));
        let ran_clone = ran.clone();
        let report = drain_queue_with_probe(
            repo,
            false,
            move |_mission_id| {
                let ran = ran_clone.clone();
                async move {
                    ran.fetch_add(1, Ordering::SeqCst);
                    Ok(0)
                }
            },
            always_proceed,
        )
        .await
        .unwrap();

        assert_eq!(
            ran.load(Ordering::SeqCst),
            0,
            "the doomed entry must not run"
        );
        assert_eq!(report.skipped, vec!["mission-dep".to_string()]);
        assert!(report.ran.is_empty());
        assert_eq!(Ticket::read_state(repo, "dependent"), TicketState::Failed);
        // The claim was finished, not re-queued (no infinite re-claim).
        assert!(queue::list(repo).is_empty());
    }

    #[tokio::test]
    async fn drain_queue_honors_once() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        for i in 0..2 {
            queue::enqueue(
                repo,
                QueueEntry {
                    mission_id: format!("mission-{i}"),
                    ticket_slug: None,
                    priority: 2,
                    seq: 0,
                },
            )
            .unwrap();
        }

        let ran = Arc::new(AtomicUsize::new(0));
        let ran_clone = ran.clone();
        let report = drain_queue_with_probe(
            repo,
            true,
            move |_mission_id| {
                let ran = ran_clone.clone();
                async move {
                    ran.fetch_add(1, Ordering::SeqCst);
                    Ok(0)
                }
            },
            always_proceed,
        )
        .await
        .unwrap();

        assert_eq!(
            ran.load(Ordering::SeqCst),
            1,
            "--once must run exactly one entry"
        );
        assert_eq!(report.ran.len(), 1);
        // One entry remains queued.
        assert_eq!(queue::list(repo).len(), 1);
    }

    #[tokio::test]
    async fn expected_front_mismatch_releases_claim_without_running() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        for mission_id in ["mission-front", "mission-expected"] {
            queue::enqueue(
                repo,
                QueueEntry {
                    mission_id: mission_id.to_string(),
                    ticket_slug: None,
                    priority: 2,
                    seq: 0,
                },
            )
            .unwrap();
        }

        let ran = Arc::new(AtomicUsize::new(0));
        let ran_clone = ran.clone();
        let report = drain_queue_with_probe_expected(
            repo,
            true,
            Some("mission-expected"),
            move |_mission_id| {
                let ran = ran_clone.clone();
                async move {
                    ran.fetch_add(1, Ordering::SeqCst);
                    Ok(0)
                }
            },
            always_proceed,
        )
        .await
        .unwrap();

        assert_eq!(ran.load(Ordering::SeqCst), 0);
        assert_eq!(report.expected_mismatch.as_deref(), Some("mission-front"));
        assert_eq!(
            queue::list(repo)
                .into_iter()
                .map(|entry| entry.mission_id)
                .collect::<Vec<_>>(),
            vec!["mission-front", "mission-expected"]
        );
    }

    #[tokio::test]
    async fn concurrent_drain_queue_once_reports_busy_without_running_second_entry() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        for i in 1..=2 {
            queue::enqueue(
                repo,
                QueueEntry {
                    mission_id: format!("mission-{i}"),
                    ticket_slug: None,
                    priority: 2,
                    seq: 0,
                },
            )
            .unwrap();
        }

        let first_runs = Arc::new(AtomicUsize::new(0));
        let release_first = Arc::new(tokio::sync::Notify::new());
        let first_repo = repo.to_path_buf();
        let first = {
            let first_runs = first_runs.clone();
            let release_first = release_first.clone();
            tokio::spawn(async move {
                drain_queue_with_probe(
                    &first_repo,
                    true,
                    move |mission_id| {
                        let first_runs = first_runs.clone();
                        let release_first = release_first.clone();
                        async move {
                            assert_eq!(mission_id, "mission-1");
                            first_runs.fetch_add(1, Ordering::SeqCst);
                            release_first.notified().await;
                            Ok(0)
                        }
                    },
                    always_proceed,
                )
                .await
            })
        };

        for _ in 0..50 {
            if first_runs.load(Ordering::SeqCst) == 1 {
                break;
            }
            tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        }
        assert_eq!(
            first_runs.load(Ordering::SeqCst),
            1,
            "first drainer should be holding the repo guard"
        );

        let second_runs = Arc::new(AtomicUsize::new(0));
        let second_runs_clone = second_runs.clone();
        let second = drain_queue_with_probe(
            repo,
            true,
            move |_mission_id| {
                let second_runs = second_runs_clone.clone();
                async move {
                    second_runs.fetch_add(1, Ordering::SeqCst);
                    Ok(0)
                }
            },
            always_proceed,
        )
        .await
        .unwrap();
        assert!(second.stopped_busy);
        assert!(second.ran.is_empty());
        assert_eq!(
            second_runs.load(Ordering::SeqCst),
            0,
            "busy loser must not run the next queued mission"
        );
        assert!(
            queue::contains(repo, "mission-2"),
            "busy loser releases its temporary claim"
        );

        release_first.notify_waiters();
        let first = first.await.unwrap().unwrap();
        assert_eq!(first.ran, vec!["mission-1".to_string()]);
        assert!(queue::contains(repo, "mission-2"));
    }

    #[tokio::test]
    async fn drain_queue_runs_a_ticket_born_entry_to_done() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();

        write_ticket(
            repo,
            "satisfiable",
            "---\ntitle: satisfiable\npriority: 2\nschedule: once\n---\n\n## Goal\nship\n",
        );
        Ticket::record_mission(repo, "satisfiable", "mission-ticket").unwrap();

        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "mission-ticket".to_string(),
                ticket_slug: Some("satisfiable".to_string()),
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();

        let ran = Arc::new(AtomicUsize::new(0));
        let ran_clone = ran.clone();
        let repo_path = repo.to_path_buf();
        let report = drain_queue_with_probe(
            repo,
            false,
            move |mission_id| {
                let ran = ran_clone.clone();
                let repo_path = repo_path.clone();
                async move {
                    assert_eq!(mission_id, "mission-ticket");
                    ran.fetch_add(1, Ordering::SeqCst);
                    write_events(
                        &repo_path,
                        &mission_id,
                        vec![created(), EventKind::MissionCompleted {}],
                    );
                    Ok(0)
                }
            },
            always_proceed,
        )
        .await
        .unwrap();

        assert_eq!(ran.load(Ordering::SeqCst), 1);
        assert_eq!(report.ran, vec!["mission-ticket".to_string()]);
        assert!(report.skipped.is_empty());
        assert_eq!(Ticket::read_state(repo, "satisfiable"), TicketState::Done);
        // The claim was retired: nothing left queued or claimed on disk.
        assert!(queue::list(repo).is_empty());
        let dir = queue::queue_dir(repo);
        let leftover: Vec<_> = std::fs::read_dir(&dir)
            .unwrap()
            .flatten()
            .filter(|e| {
                e.path()
                    .file_name()
                    .and_then(|n| n.to_str())
                    .is_some_and(|n| n.contains(".claimed."))
            })
            .collect();
        assert!(leftover.is_empty(), "claim file was not retired");
    }

    /// The mission-id approve path (Slack `/kranz approve m-…`) enqueues a bare
    /// entry with `ticket_slug: None`; drain must resolve the linked ticket via
    /// the reverse lookup so its pipeline state still advances to Done. Pins the
    /// `.or_else(Ticket::slug_for_mission)` fallback — without it, a bare entry
    /// runs but the ticket is left stuck in Review.
    #[tokio::test]
    async fn drain_queue_resolves_a_bare_entry_to_its_linked_ticket() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();

        write_ticket(
            repo,
            "linked",
            "---\ntitle: linked\npriority: 2\nschedule: once\n---\n\n## Goal\nship\n",
        );
        // Link the ticket to the mission and park it mid-pipeline, exactly as
        // the Slack approve-by-mission-id flow leaves it.
        Ticket::record_mission(repo, "linked", "m-linked").unwrap();
        Ticket::write_state(repo, "linked", TicketState::Queued, None).unwrap();

        // A sibling ticket linked to a DIFFERENT mission must not be resolved.
        write_ticket(
            repo,
            "other",
            "---\ntitle: other\npriority: 2\nschedule: once\n---\n\n## Goal\nnope\n",
        );
        Ticket::record_mission(repo, "other", "m-other").unwrap();

        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "m-linked".to_string(),
                ticket_slug: None, // bare: the fallback must find "linked"
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();

        let repo_path = repo.to_path_buf();
        let report = drain_queue_with_probe(
            repo,
            false,
            move |mission_id| {
                let repo_path = repo_path.clone();
                async move {
                    assert_eq!(mission_id, "m-linked");
                    write_events(
                        &repo_path,
                        &mission_id,
                        vec![created(), EventKind::MissionCompleted {}],
                    );
                    Ok(0)
                }
            },
            always_proceed,
        )
        .await
        .unwrap();

        assert_eq!(report.ran, vec!["m-linked".to_string()]);
        assert_eq!(
            Ticket::read_state(repo, "linked"),
            TicketState::Done,
            "the linked ticket must advance via the reverse lookup"
        );
        assert_eq!(
            Ticket::read_state(repo, "other"),
            TicketState::Drafting,
            "an unrelated ticket must be untouched (record_mission left it Drafting)"
        );
        assert!(queue::list(repo).is_empty());
    }

    #[tokio::test]
    async fn drain_queue_parks_ticket_when_readiness_fails_after_claim() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();

        write_ticket(
            repo,
            "need-auth",
            "---\ntitle: need-auth\npriority: 2\nschedule: once\n---\n\n## Goal\nship\n",
        );
        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "m-park".to_string(),
                ticket_slug: Some("need-auth".to_string()),
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();

        let ran = Arc::new(AtomicUsize::new(0));
        let ran_clone = ran.clone();
        let report = drain_queue_with_probe(
            repo,
            false,
            move |_mission_id| {
                let ran = ran_clone.clone();
                async move {
                    ran.fetch_add(1, Ordering::SeqCst);
                    Ok(0)
                }
            },
            |_repo, id| Ok(park_report(id)),
        )
        .await
        .unwrap();

        assert_eq!(ran.load(Ordering::SeqCst), 0);
        assert_eq!(report.parked, vec!["m-park".to_string()]);
        assert!(report.ran.is_empty());
        assert_eq!(Ticket::read_state(repo, "need-auth"), TicketState::Parked);
        assert!(queue::list(repo).is_empty());
    }

    #[tokio::test]
    async fn drain_queue_preserves_ticketless_entry_when_readiness_parks() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();
        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "m-external".to_string(),
                ticket_slug: None,
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();

        let report = drain_queue_with_probe(
            repo,
            false,
            |_mission_id| async { panic!("parked ticketless mission must not run") },
            |_repo, id| Ok(park_report(id)),
        )
        .await
        .unwrap();

        assert_eq!(report.parked, vec!["m-external".to_string()]);
        assert_eq!(
            queue::list(repo)
                .into_iter()
                .map(|entry| entry.mission_id)
                .collect::<Vec<_>>(),
            vec!["m-external"]
        );
    }

    #[tokio::test]
    async fn drain_queue_rate_limit_rotates_then_parks_after_cap() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();

        write_ticket(
            repo,
            "limited",
            "---\ntitle: limited\npriority: 2\nschedule: once\n---\n\n## Goal\na\n",
        );
        write_ticket(
            repo,
            "sibling",
            "---\ntitle: sibling\npriority: 2\nschedule: once\n---\n\n## Goal\nb\n",
        );
        Ticket::record_mission(repo, "sibling", "m-sibling").unwrap();
        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "m-limited".to_string(),
                ticket_slug: Some("limited".to_string()),
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();
        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "m-sibling".to_string(),
                ticket_slug: Some("sibling".to_string()),
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();

        // Rate-limit sleep is clamped under cfg(test) in drain_queue_with_probe.
        let probe = |_repo: &Path, id: &str| {
            if id == "m-limited" {
                Ok(rate_limited_report(id))
            } else {
                Ok(proceed_report(id))
            }
        };

        let ran = Arc::new(AtomicUsize::new(0));
        let ran_clone = ran.clone();
        let repo_path = repo.to_path_buf();
        let report = drain_queue_with_probe(
            repo,
            false,
            move |mission_id| {
                let ran = ran_clone.clone();
                let repo_path = repo_path.clone();
                async move {
                    assert_eq!(mission_id, "m-sibling");
                    ran.fetch_add(1, Ordering::SeqCst);
                    write_events(
                        &repo_path,
                        &mission_id,
                        vec![created(), EventKind::MissionCompleted {}],
                    );
                    Ok(0)
                }
            },
            probe,
        )
        .await
        .unwrap();

        assert_eq!(ran.load(Ordering::SeqCst), 1);
        assert_eq!(report.ran, vec!["m-sibling".to_string()]);
        assert_eq!(report.parked, vec!["m-limited".to_string()]);
        assert_eq!(Ticket::read_state(repo, "sibling"), TicketState::Done);
        assert_eq!(Ticket::read_state(repo, "limited"), TicketState::Parked);
        assert!(queue::list(repo).is_empty());
    }

    /// Pins the drain loop's terminal ticket write to `reconcile_ticket_for_mission`
    /// (not a local exit-code mapping): a mission that folds to Complete leaves
    /// its ticket Done, while one that folds to Blocked leaves it NeedsContext —
    /// never Failed, even though `run_mission` still returns `Ok(0)` in both cases.
    #[tokio::test]
    async fn drain_reconciles_terminal_ticket_via_helper() {
        let tmp = tempfile::tempdir().unwrap();
        let repo = tmp.path();

        write_ticket(
            repo,
            "done-ticket",
            "---\ntitle: done\npriority: 2\nschedule: once\n---\n\n## Goal\nship\n",
        );
        write_ticket(
            repo,
            "blocked-ticket",
            "---\ntitle: blocked\npriority: 2\nschedule: once\n---\n\n## Goal\nship\n",
        );
        Ticket::record_mission(repo, "done-ticket", "m-done").unwrap();
        Ticket::record_mission(repo, "blocked-ticket", "m-blocked").unwrap();

        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "m-done".to_string(),
                ticket_slug: Some("done-ticket".to_string()),
                priority: 2,
                seq: 0,
            },
        )
        .unwrap();
        queue::enqueue(
            repo,
            QueueEntry {
                mission_id: "m-blocked".to_string(),
                ticket_slug: Some("blocked-ticket".to_string()),
                priority: 2,
                seq: 1,
            },
        )
        .unwrap();

        let repo_path = repo.to_path_buf();
        let report = drain_queue_with_probe(
            repo,
            false,
            move |mission_id| {
                let repo_path = repo_path.clone();
                async move {
                    if mission_id == "m-done" {
                        write_events(
                            &repo_path,
                            &mission_id,
                            vec![created(), EventKind::MissionCompleted {}],
                        );
                    } else {
                        write_events(
                            &repo_path,
                            &mission_id,
                            vec![
                                created(),
                                EventKind::PlanApproved {
                                    plan: plan_with_one_milestone(),
                                    base_sha: None,
                                },
                                EventKind::MilestoneBlocked {
                                    block_context: None,
                                    milestone_id: "ms-1".to_string(),
                                    reason: "needs input".to_string(),
                                },
                            ],
                        );
                    }
                    // Both missions return Ok(0): the exit code must not
                    // determine the ticket's terminal state any more.
                    Ok(0)
                }
            },
            always_proceed,
        )
        .await
        .unwrap();

        assert_eq!(report.ran.len(), 2);
        assert_eq!(Ticket::read_state(repo, "done-ticket"), TicketState::Done);
        assert_eq!(
            Ticket::read_state(repo, "blocked-ticket"),
            TicketState::NeedsContext
        );
    }
}