thirdshift 0.8.1

Turn a GitHub issue into a ready-for-review pull request, unattended
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
//! A Spec run: each of a Spec's Tickets, in dependency order and several at
//! once, taken by a Ticket's Run, a Merge run into the Spec branch in a child `thirdshift`
//! (ADR-0006), then the Spec review and the Spec PR from the Spec branch into
//! the Base branch, kept mergeable and green like a Run's PR, and Self-merged
//! when the Spec run was asked to merge.

use std::collections::hash_map::Entry;
use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};
use std::num::NonZeroUsize;
use std::path::PathBuf;
use std::process::Child;
use std::sync::mpsc::{self, Sender};
use std::thread;

use anyhow::{Context, Result, bail};

use crate::base_fix::{BaseFix, BaseFixAsk};
use crate::child_run::{self, Ended, Kind};
use crate::failed_run::{self, FailedRun};
use crate::github::{self, PullRequest, Ticket};
use crate::interrupt;
use crate::issue::IssueUrl;
use crate::plugin::Plugin;
use crate::progress;
use crate::prompt;
use crate::run::{self, Goal, Reached};
use crate::session::{Logs, Sessions};
use crate::worktree::Worktree;

/// The Spec review session's kind, in its progress lines and log name.
const SPEC_REVIEW: &str = "spec-review";

/// The markers around the Tickets checklist in the Spec PR's body, so it can
/// be replaced without touching the text around it.
const CHECKLIST_START: &str = "<!-- thirdshift:tickets -->";
const CHECKLIST_END: &str = "<!-- /thirdshift:tickets -->";

/// Whether an issue has Tickets, as a Spec has, and every one is closed:
/// `open` says of each of its sub-issues whether it is open.
pub fn all_closed(open: impl IntoIterator<Item = bool>) -> bool {
    let mut open = open.into_iter().peekable();
    open.peek().is_some() && open.all(|is_open| !is_open)
}

/// Take the Spec `spec`, whose Tickets were last read as `tickets`, from
/// its Spec branch, checked out in `worktree`, to a Spec PR into `base` that
/// reaches `goal`. The Spec branch is pushed before any Ticket starts, and
/// up to `parallel` Tickets run at once, the graph read again whenever one
/// ends. The Spec PR is opened as a draft once the first Ticket lands, or
/// turned back into a draft if it is already open, and its Tickets checklist rewritten
/// as Tickets start and end. A Ticket that fails stops only the Tickets it
/// blocks; with any Ticket not done, this is a Failed spec run, which leaves
/// the Spec PR, if there is one, a draft. An interrupt ends it too, once the
/// running Tickets' Runs have ended, starting nothing more. Once every Ticket
/// has landed, the Spec review, logged in `logs`, reviews the Spec branch,
/// then the Tickets checklist is put back in the Spec PR's body and the Spec
/// PR is marked ready. The Spec PR then goes through the same Repair
/// loop as a Run's PR, and for [`Goal::Merged`] the Self-merge. A failure
/// from the Spec review on goes through the Failed run path. The worktree is
/// cleaned up when this returns, or kept by the Failed run path if its work
/// did not reach origin. However it ends, it carries a line on each Ticket
/// it landed or did not get done, for the Run notification. `base_fix` is for
/// the Spec PR's Repair loop, and if it may start a Base fix, so may each
/// Ticket's Run, into the Spec branch.
#[allow(clippy::too_many_arguments)]
pub fn run(
    spec: &IssueUrl,
    tickets: Vec<Ticket>,
    worktree: Worktree,
    base: &str,
    goal: Goal,
    base_fix: &mut BaseFix,
    logs: &Logs,
    parallel: NonZeroUsize,
) -> Result<Reached, FailedRun> {
    let mut spec_pr = draft_spec_pr(spec, worktree.branch())?;
    let (ticket_lines, landed) = land_tickets(
        spec,
        tickets,
        &worktree,
        base,
        parallel,
        &base_fix.ask_of_tickets(),
        &mut spec_pr,
    );
    let ended = match landed {
        Ok(checklist) => open_and_review(
            spec, worktree, base, spec_pr, &checklist, goal, base_fix, logs,
        ),
        Err(error) => Err(FailedRun {
            pr_url: spec_pr.map(|pr| pr.url),
            ..FailedRun::from(error)
        }),
    };
    match ended {
        Ok(reached) => Ok(Reached {
            ticket_lines,
            ..reached
        }),
        Err(failed) => Err(FailedRun {
            ticket_lines,
            ..failed
        }),
    }
}

/// Once every Ticket has landed: open the Spec PR as a draft with
/// `checklist` if `spec_pr` is none, run the Spec review, put `checklist`
/// back, mark the Spec PR ready and take it to `goal`, as [`run`] does.
#[allow(clippy::too_many_arguments)]
fn open_and_review(
    spec: &IssueUrl,
    worktree: Worktree,
    base: &str,
    spec_pr: Option<PullRequest>,
    checklist: &str,
    goal: Goal,
    base_fix: &mut BaseFix,
    logs: &Logs,
) -> Result<Reached, FailedRun> {
    let spec_pr = match spec_pr {
        Some(pr) => pr,
        None => open_spec_pr(spec, worktree.branch(), base, checklist)?,
    };
    let mut log = logs.path(SPEC_REVIEW);
    let delivered = review_and_deliver(
        spec, &worktree, base, &spec_pr, checklist, goal, base_fix, logs, &mut log,
    );
    match delivered {
        Ok(()) => Ok(Reached {
            pr_url: spec_pr.url,
            goal,
            log: Some(log),
            ticket_lines: Vec::new(),
        }),
        Err(error) => {
            // The Spec review may have rewritten the body without it.
            write_checklist_or_warn(spec, &spec_pr, checklist);
            Err(failed_run::fail(spec, worktree, base, &log, error))
        }
    }
}

/// Where a Ticket's Run in this Spec run stands.
enum TicketOutcome {
    /// It has started and not yet ended.
    Running,
    /// Its PR, as the Run printed it, and what became of the Base fix it
    /// took, if any, as the Run reported it.
    Landed {
        pr_url: Option<String>,
        base_fix: Option<String>,
    },
    /// Why, and its session log, as the Run reported them.
    Failed { cause: String, log: Option<String> },
    /// Ended by an interrupt passed on to it.
    Interrupted,
}

/// Push the Spec branch, then keep up to `parallel` Tickets running, each
/// time one ends starting ready ones, lowest number first, until none is
/// ready and none is running, keeping the Tickets checklist of `spec_pr` up
/// to date as far as GitHub lets it, and opening it into `base` once a Ticket
/// lands if there is none. Returns a line on each Ticket that landed or is
/// not done, none if the Spec branch could not be pushed, and the last
/// Tickets checklist if every Ticket is done: if not, it fails, after putting
/// those lines on stderr unless an interrupt or another error ended it first.
/// Each Ticket's Run may start a Base fix if `base_fix` allows one.
fn land_tickets(
    spec: &IssueUrl,
    mut tickets: Vec<Ticket>,
    worktree: &Worktree,
    base: &str,
    parallel: NonZeroUsize,
    base_fix: &BaseFixAsk,
    spec_pr: &mut Option<PullRequest>,
) -> (Vec<String>, Result<String>) {
    if let Err(error) = worktree.push() {
        return (Vec::new(), Err(error));
    }
    let mut outcomes = BTreeMap::new();
    let landing = run_ready_tickets(
        spec,
        &mut tickets,
        &mut outcomes,
        worktree.branch(),
        base,
        parallel,
        base_fix,
        spec_pr,
    );
    let lines = summarize(&tickets, &outcomes);
    let landed = landing.and_then(|()| {
        let not_done: Vec<String> = tickets
            .iter()
            .filter(|ticket| ticket.is_open)
            .map(|ticket| format!("#{}", ticket.number))
            .collect();
        if not_done.is_empty() {
            return Ok(checklist(&tickets, &outcomes));
        }
        for line in &lines {
            progress::step(line);
        }
        bail!("Tickets not done: {}", not_done.join(", "));
    });
    (lines, landed)
}

/// Keep up to `parallel` ready Tickets running, lowest number first, until
/// none is ready and none is running, keeping `tickets` as last read, each
/// Ticket's outcome in `outcomes`, and the Tickets checklist of `spec_pr`,
/// opened into `base` once a Ticket lands, up to date. An interrupt, or an
/// error other than a Ticket failing, stops any more from starting, and
/// fails this once those running have ended. Each Ticket's Run may start a
/// Base fix if `base_fix` allows one.
#[allow(clippy::too_many_arguments)]
fn run_ready_tickets(
    spec: &IssueUrl,
    tickets: &mut Vec<Ticket>,
    outcomes: &mut BTreeMap<u64, TicketOutcome>,
    spec_branch: &str,
    base: &str,
    parallel: NonZeroUsize,
    base_fix: &BaseFixAsk,
    spec_pr: &mut Option<PullRequest>,
) -> Result<()> {
    let (ended, endings) = mpsc::channel();
    let mut running = HashSet::new();
    let mut error = None;
    loop {
        let mut started = false;
        while error.is_none() && !interrupt::requested() && running.len() < parallel.get() {
            let Some(ticket) = next_ready(tickets, outcomes, &running) else {
                break;
            };
            match start_ticket(spec, ticket, spec_branch, base_fix, ended.clone()) {
                Ok(()) => {
                    running.insert(ticket);
                    outcomes.insert(ticket, TicketOutcome::Running);
                    started = true;
                }
                Err(start_error) => error = Some(start_error),
            }
        }
        if started && let Some(pr) = spec_pr {
            write_checklist_or_warn(spec, pr, &checklist(tickets, outcomes));
        }
        if running.is_empty() {
            break;
        }
        let (ticket, result) = endings
            .recv()
            .expect("a running Ticket's thread holds a sender");
        running.remove(&ticket);
        // Once there is an error, the rest are only waited for, but each
        // still has its line in the checklist, from the last graph read.
        let outcome = result.unwrap_or_else(|finish_error| {
            let cause = format!("{finish_error:#}");
            error.get_or_insert(finish_error);
            TicketOutcome::Failed { cause, log: None }
        });
        let landed = matches!(outcome, TicketOutcome::Landed { .. });
        outcomes.insert(ticket, outcome);
        match github::tickets(spec) {
            Ok(reread) => *tickets = reread,
            Err(reread_error) => {
                error.get_or_insert(reread_error);
            }
        }
        let list = checklist(tickets, outcomes);
        match spec_pr {
            Some(pr) => write_checklist_or_warn(spec, pr, &list),
            None if landed => match open_spec_pr(spec, spec_branch, base, &list) {
                Ok(pr) => *spec_pr = Some(pr),
                Err(open_error) => {
                    error.get_or_insert(open_error);
                }
            },
            None => {}
        }
    }
    if let Some(error) = error {
        return Err(error);
    }
    if interrupt::requested() {
        bail!("interrupted");
    }
    Ok(())
}

/// The lowest-numbered Ticket that is open, not an Unready Ticket, has no
/// sub-issues, has every blocker closed and none `running`, isn't `running`
/// and has no outcome in `outcomes` yet. A blocker's Run closes its issue
/// before it ends, so a closed blocker may still be running.
fn next_ready(
    tickets: &[Ticket],
    outcomes: &BTreeMap<u64, TicketOutcome>,
    running: &HashSet<u64>,
) -> Option<u64> {
    tickets
        .iter()
        .filter(|ticket| {
            ticket.is_open
                && !ticket.has_sub_issues
                && ticket.open_blockers.is_empty()
                && !ticket
                    .blockers
                    .iter()
                    .any(|blocker| running.contains(blocker))
                && !outcomes.contains_key(&ticket.number)
                && !running.contains(&ticket.number)
                && ticket.labels.unready().is_none()
        })
        .map(|ticket| ticket.number)
        .min()
}

/// A line on each Ticket that landed in this Spec run, with its PR, and on
/// each Ticket not done, with why, lowest number first.
fn summarize(tickets: &[Ticket], outcomes: &BTreeMap<u64, TicketOutcome>) -> Vec<String> {
    let mut lines = Vec::new();
    for ticket in by_number(tickets) {
        let number = ticket.number;
        if !ticket.is_open && !outcomes.contains_key(&number) {
            continue;
        }
        let standing = standing(ticket, tickets, outcomes);
        lines.push(match outcomes.get(&number) {
            Some(TicketOutcome::Failed { log: Some(log), .. }) if ticket.is_open => {
                format!("#{number} {standing} (session log: {log})")
            }
            _ => format!("#{number} {standing}"),
        });
    }
    lines
}

/// `tickets`, lowest number first.
fn by_number(tickets: &[Ticket]) -> Vec<&Ticket> {
    let mut sorted: Vec<&Ticket> = tickets.iter().collect();
    sorted.sort_by_key(|ticket| ticket.number);
    sorted
}

/// Where `ticket`, one of `tickets`, stands in this Spec run, as in
/// "#21 <standing>": done, with its PR if it landed in this Spec run, or why
/// it is not done.
fn standing(
    ticket: &Ticket,
    tickets: &[Ticket],
    outcomes: &BTreeMap<u64, TicketOutcome>,
) -> String {
    let number = ticket.number;
    let landed = match outcomes.get(&number) {
        Some(TicketOutcome::Landed { pr_url, base_fix }) => {
            let mut landed = "landed".to_string();
            if let Some(pr_url) = pr_url {
                landed += &format!(" with {pr_url}");
            }
            if let Some(base_fix) = base_fix {
                landed += &format!(", after Base fix {base_fix}");
            }
            Some(landed)
        }
        _ => None,
    };
    if !ticket.is_open {
        return landed.unwrap_or_else(|| "done".to_string());
    }
    match outcomes.get(&number) {
        Some(TicketOutcome::Running) => return "running".to_string(),
        Some(TicketOutcome::Interrupted) => return "interrupted".to_string(),
        Some(TicketOutcome::Failed { cause, .. }) => return format!("failed: {cause}"),
        _ => {}
    }
    if let Some(landed) = landed {
        format!("{landed}, but is still open")
    } else if let Some(label) = ticket.labels.unready() {
        format!("unready: labelled {label}")
    } else if ticket.has_sub_issues {
        "unready: has sub-issues".to_string()
    } else if let Some(cycle) = cycle_through(number, tickets) {
        let cycle: Vec<String> = cycle.iter().map(|number| format!("#{number}")).collect();
        format!("in a cycle: {}", cycle.join(" blocked by "))
    } else if ticket.open_blockers.is_empty() {
        // Ready, but not started yet, or the Spec run ended before it could.
        "not started".to_string()
    } else {
        let blockers: Vec<String> = ticket
            .open_blockers
            .iter()
            .map(|blocker| {
                if tickets.iter().any(|ticket| ticket.number == *blocker) {
                    format!("#{blocker}")
                } else {
                    format!("#{blocker} (outside the Spec)")
                }
            })
            .collect();
        format!("blocked by {}", blockers.join(", "))
    }
}

/// The Tickets checklist, between its markers: a line on each of
/// `tickets`, lowest number first, ticked if it is done, with where it
/// stands.
fn checklist(tickets: &[Ticket], outcomes: &BTreeMap<u64, TicketOutcome>) -> String {
    let mut list = format!("{CHECKLIST_START}\n## Tickets\n\n");
    for ticket in by_number(tickets) {
        let tick = if ticket.is_open { ' ' } else { 'x' };
        list += &format!(
            "- [{tick}] #{} {}\n",
            ticket.number,
            standing(ticket, tickets, outcomes)
        );
    }
    list + CHECKLIST_END + "\n"
}

/// `body` with its Tickets checklist, the text from its first start marker
/// to the next end marker, replaced by `checklist`, or with `checklist`
/// appended if it has no such pair of markers.
fn with_checklist(body: &str, checklist: &str) -> String {
    if let Some(start) = body.find(CHECKLIST_START)
        && let Some(end) = body[start..].find(CHECKLIST_END)
    {
        let end = start + end + CHECKLIST_END.len();
        let end = if body[end..].starts_with('\n') {
            end + 1
        } else {
            end
        };
        return format!("{}{checklist}{}", &body[..start], &body[end..]);
    }
    if body.is_empty() {
        return checklist.to_string();
    }
    format!("{}\n\n{checklist}", body.trim_end())
}

/// The shortest cycle of "blocked by" links among `tickets` from Ticket
/// `start` back to itself, `start` first and last, if there is one.
fn cycle_through(start: u64, tickets: &[Ticket]) -> Option<Vec<u64>> {
    let blockers = |number: u64| {
        tickets
            .iter()
            .find(|ticket| ticket.number == number)
            .map_or(&[][..], |ticket| &ticket.open_blockers[..])
    };
    let mut reached_from = HashMap::new();
    let mut queue = VecDeque::from([start]);
    while let Some(number) = queue.pop_front() {
        for &blocker in blockers(number) {
            if blocker == start {
                let mut cycle = vec![start];
                let mut at = number;
                while at != start {
                    cycle.push(at);
                    at = reached_from[&at];
                }
                cycle.push(start);
                cycle.reverse();
                return Some(cycle);
            }
            if let Entry::Vacant(entry) = reached_from.entry(blocker) {
                entry.insert(number);
                queue.push_back(blocker);
            }
        }
    }
    None
}

/// Start Ticket `number` of `spec` as a child `thirdshift`, a Merge run into
/// `spec_branch` from the same Launch directory that may start a Base fix if
/// `base_fix` allows one, and a thread that sends `number` and how it ended
/// on `ended`.
fn start_ticket(
    spec: &IssueUrl,
    number: u64,
    spec_branch: &str,
    base_fix: &BaseFixAsk,
    ended: Sender<(u64, Result<TicketOutcome>)>,
) -> Result<()> {
    progress::step(format_args!("starting #{number}"));
    let kind = Kind::Ticket {
        spec_branch: spec_branch.to_string(),
    };
    let child = child_run::start(&spec.sibling(number), &kind, base_fix)?;
    thread::spawn(move || {
        let _ = ended.send((number, finish_ticket(number, child)));
    });
    Ok(())
}

/// Wait for Ticket `number`'s Run `child`, relaying its stderr, and say how
/// it ended.
fn finish_ticket(number: u64, child: Child) -> Result<TicketOutcome> {
    Ok(match child_run::wait(number, child)? {
        Ended::Reached { pr_url, base_fix } => {
            progress::step(format_args!("#{number} landed"));
            TicketOutcome::Landed { pr_url, base_fix }
        }
        Ended::Interrupted => {
            progress::step(format_args!("#{number} interrupted"));
            TicketOutcome::Interrupted
        }
        Ended::Failed { cause, log } => {
            progress::step(format_args!("#{number} failed"));
            TicketOutcome::Failed { cause, log }
        }
    })
}

/// The open Spec PR from `spec_branch`, if there is one, converted back to a
/// draft while the Tickets run.
fn draft_spec_pr(spec: &IssueUrl, spec_branch: &str) -> Result<Option<PullRequest>> {
    let Some(pr) = github::pull_request_for(spec, spec_branch)?.filter(|pr| pr.is_open()) else {
        return Ok(None);
    };
    if !pr.is_draft {
        github::convert_to_draft(spec, spec_branch)?;
    }
    Ok(Some(pr))
}

/// Open the Spec PR from `spec_branch` into `base` as a draft, titled from
/// the Spec, closing it, with `checklist` as its Tickets checklist.
fn open_spec_pr(
    spec: &IssueUrl,
    spec_branch: &str,
    base: &str,
    checklist: &str,
) -> Result<PullRequest> {
    progress::step(format_args!("opening the Spec PR into {base} as a draft"));
    let title = github::issue_title(spec)?;
    let body = format!(
        "The work on Spec #{number}, gathered from its Tickets on {spec_branch}.\n\n\
         Closes #{number}\n\n\
         {checklist}",
        number = spec.number
    );
    github::create_draft_pr(spec, spec_branch, base, &title, &body)?;
    github::pull_request_for(spec, spec_branch)?.context("the Spec PR just opened is not found")
}

/// Put `checklist` in the body of the Spec PR `pr`, in place of its Tickets
/// checklist, leaving the rest of the body as it is.
fn write_checklist(spec: &IssueUrl, pr: &PullRequest, checklist: &str) -> Result<()> {
    let body = github::pr_body(spec, pr.number)?;
    let updated = with_checklist(&body, checklist);
    if updated != body {
        progress::step("updating the Spec PR's Tickets checklist");
        github::set_pr_body(spec, pr.number, &updated)?;
    }
    Ok(())
}

/// [`write_checklist`], only warning if it fails: the Spec run goes on
/// without it.
fn write_checklist_or_warn(spec: &IssueUrl, pr: &PullRequest, checklist: &str) {
    if let Err(error) = write_checklist(spec, pr, checklist) {
        progress::step(format_args!(
            "could not update the Spec PR's Tickets checklist: {error:#}"
        ));
    }
}

/// Bring the Spec branch in `worktree` up to date with the Tickets landed on
/// origin, and run the Spec review on it. Then push the Spec branch, for any
/// commit the session left unpushed, put `checklist` back in the body the
/// session wrote for the Spec PR `spec_pr` into `base`, mark it ready, and
/// [`run::deliver`] it to `goal`, with the Spec as the issue and `base_fix`
/// as its one Base fix. `log` is left at the most recent session's log.
#[allow(clippy::too_many_arguments)]
fn review_and_deliver(
    spec: &IssueUrl,
    worktree: &Worktree,
    base: &str,
    spec_pr: &PullRequest,
    checklist: &str,
    goal: Goal,
    base_fix: &mut BaseFix,
    logs: &Logs,
    log: &mut PathBuf,
) -> Result<()> {
    let spec_branch = worktree.branch();
    worktree.fast_forward_to_origin()?;
    let plugin = Plugin::write()?;
    Sessions::within(logs, worktree.path(), plugin.path(), |sessions| {
        let mut run_session = |kind: &str, prompt: &str| sessions.run(kind, prompt, log);
        run_session(
            SPEC_REVIEW,
            &prompt::spec_review(spec, base, spec_branch, &spec_pr.url),
        )?;
        worktree.push()?;
        write_checklist(spec, spec_pr, checklist)?;
        let pr = run::mark_pr_ready(spec, spec_branch, base)?;
        run::deliver(spec, worktree, base, &pr, goal, base_fix, &mut run_session)
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::labels::{Label, Labels, READY_FOR_AGENT, UNREADY};

    fn ticket(number: u64, is_open: bool, blockers: &[u64], open_blockers: &[u64]) -> Ticket {
        Ticket {
            number,
            is_open,
            labels: Labels::default(),
            has_sub_issues: false,
            blockers: blockers.to_vec(),
            open_blockers: open_blockers.to_vec(),
        }
    }

    #[test]
    fn a_ticket_waits_for_a_blocker_whose_run_closed_its_issue_but_has_not_ended() {
        // #22's Run closed #22 but is still cleaning up; #21 has landed.
        let tickets = [
            ticket(21, false, &[], &[]),
            ticket(22, false, &[], &[]),
            ticket(23, true, &[21, 22], &[]),
        ];
        let outcomes = BTreeMap::from([(
            21,
            TicketOutcome::Landed {
                pr_url: None,
                base_fix: None,
            },
        )]);

        assert_eq!(next_ready(&tickets, &outcomes, &HashSet::from([22])), None);
        assert_eq!(next_ready(&tickets, &outcomes, &HashSet::new()), Some(23));
    }

    #[test]
    fn each_unready_label_keeps_a_ticket_from_running_and_is_named_in_its_line() {
        for label in UNREADY {
            let mut unready = ticket(21, true, &[], &[]);
            unready.labels = [READY_FOR_AGENT, label]
                .map(Label::name)
                .into_iter()
                .collect();
            let tickets = [unready, ticket(22, true, &[], &[])];
            let outcomes = BTreeMap::new();

            assert_eq!(
                next_ready(&tickets, &outcomes, &HashSet::new()),
                Some(22),
                "{label}"
            );
            assert_eq!(
                summarize(&tickets, &outcomes)[0],
                format!("#21 unready: labelled {label}")
            );
        }
    }

    #[test]
    fn a_failed_tickets_line_says_its_cause() {
        let tickets = [ticket(23, true, &[], &[])];
        let cause = "git fetch origin issue-21 failed: \
                     error: fetching ref refs/remotes/origin/issue-21 failed";
        let outcomes = BTreeMap::from([(
            23,
            TicketOutcome::Failed {
                cause: cause.to_string(),
                log: None,
            },
        )]);

        assert_eq!(
            summarize(&tickets, &outcomes),
            ["#23 failed: git fetch origin issue-21 failed: \
              error: fetching ref refs/remotes/origin/issue-21 failed"]
        );
    }

    #[test]
    fn the_checklist_ticks_done_tickets_and_says_where_each_other_stands() {
        let mut unready = ticket(26, true, &[], &[]);
        unready.labels = ["needs-info"].into_iter().collect();
        let tickets = [
            ticket(23, true, &[], &[]),
            ticket(21, false, &[], &[]),
            ticket(22, false, &[], &[]),
            ticket(24, true, &[23], &[23]),
            ticket(25, true, &[], &[]),
            unready,
            ticket(27, true, &[], &[]),
        ];
        let outcomes = BTreeMap::from([
            (
                21,
                TicketOutcome::Landed {
                    pr_url: Some("https://x/pull/1".to_string()),
                    base_fix: None,
                },
            ),
            (
                23,
                TicketOutcome::Failed {
                    cause: "claude exited 1".to_string(),
                    log: Some("/logs/23.jsonl".to_string()),
                },
            ),
            (25, TicketOutcome::Running),
        ]);

        assert_eq!(
            checklist(&tickets, &outcomes),
            "<!-- thirdshift:tickets -->\n\
             ## Tickets\n\
             \n\
             - [x] #21 landed with https://x/pull/1\n\
             - [x] #22 done\n\
             - [ ] #23 failed: claude exited 1\n\
             - [ ] #24 blocked by #23\n\
             - [ ] #25 running\n\
             - [ ] #26 unready: labelled needs-info\n\
             - [ ] #27 not started\n\
             <!-- /thirdshift:tickets -->\n"
        );
    }

    const LIST: &str = "<!-- thirdshift:tickets -->\nnew\n<!-- /thirdshift:tickets -->\n";

    #[test]
    fn the_checklist_replaces_the_one_between_the_markers_and_leaves_the_rest() {
        let body = "Intro.\n\n<!-- thirdshift:tickets -->\nold\n<!-- /thirdshift:tickets -->\n\nCloses #20\n";

        assert_eq!(
            with_checklist(body, LIST),
            format!("Intro.\n\n{LIST}\nCloses #20\n")
        );
    }

    #[test]
    fn the_checklist_is_appended_when_the_markers_are_gone() {
        assert_eq!(
            with_checklist("A rewritten body.\n\nCloses #20\n", LIST),
            format!("A rewritten body.\n\nCloses #20\n\n{LIST}")
        );
        assert_eq!(with_checklist("", LIST), LIST);
    }

    #[test]
    fn the_checklist_is_appended_when_only_one_marker_is_left() {
        let body = "Text\n<!-- /thirdshift:tickets -->\n<!-- thirdshift:tickets -->\nmore";

        assert_eq!(with_checklist(body, LIST), format!("{body}\n\n{LIST}"));
    }
}