mahbot 0.11.1

An autonomous agentic engineering system that manages software development through role separation, subagents, and deterministic diagnostics.
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
//! The `verification` stage runs the workspace's project commands, the read-only code
//! reviewers and the single functional tester at the same time on one phase job, and the
//! round produces ONE consolidated comment and one advance/bounce decision. The project
//! commands are nobody's roster slot: they are the round's own work, run in parallel with
//! the participants. A failing command's record carries the part of its output that names the
//! failure ([`failed_command_record`]), so the next round can tell what broke.
//!
//! The round's cohorts are told apart by their stored `agents.kind` labels, never by slot
//! order, and a roster that is not the round's shape is never resumed as one. The
//! identical-content skip, the churn-calibrated reviewer count and the reviewed-base
//! recording are reviewer-only: the tester is never skipped.

use std::fmt::Write as _;
use std::path::Path;
use std::sync::Arc;

use crate::git::commands::{
    has_unstaged_changes, run_git_add_all, run_git_head, run_git_status, run_git_worktree_snapshot,
    run_git_write_tree,
};
use crate::pipeline::board::Ticket;
use crate::prompt::{load_prompt, load_prompt_sections, substitute};
use crate::tools::shell::{ShellMode, ShellTool};
use crate::util::excerpt::failure_excerpt;
use crate::{DiagnosticsCommands, Role, Workspace};

use super::{
    AgentSlot, ExtractionMode, FinalizeOutcome, ParallelVerdict, TicketPhase, TransitionCtx,
    agent_slot_from_roster_row, bounce_to_development, build_agent_slots, build_round_grouping,
    comment_and_transition, debug, guard_job_phase, info, insert_round_slots, pause_freezing,
    read_roster_or_bail, rearm_resumed_slots, render_joint_comment, reset_phase_attempt,
    run_parallel_agents, sync_phase_job_task, warn,
};

/// The stage's own name: the role the consolidated round comment is authored under, and the
/// label in log fields. (The recorded transition actor is the phase token.)
pub(crate) const STAGE: &str = "Verification";

const ACTIVE_PHASE: TicketPhase = TicketPhase::Verification;

const SUCCESS_PHASE: TicketPhase = TicketPhase::InSanitation;

/// Minimum acceptable score (0-10) for a verification verdict.
const VERIFICATION_THRESHOLD: u8 = 9;

/// The functional tester runs exactly one agent per round — the round's functional check,
/// which no round may run without.
const TESTER_COUNT: usize = 1;

/// Rendered at the head of the consolidated comment of a round whose reviewer cohort was not
/// dispatched: the skipped code review is part of the round's result. It ends with the blank
/// line that separates it from the round's own sections.
const SKIPPED_REVIEW_NOTE: &str = "\
**Code review skipped** — the content is identical to what the reviewers were \
last handed on this ticket (same commit, same working tree, no changes since), \
so they were not dispatched again. The functional check still ran.\n\n";

/// Static metadata for one participant kind of a verification round. The code reviewers and
/// the tester differ only here: the role (which selects the system prompt, tools and shell
/// mode), the roster kind their slots are stored under, the work prompt, the angle supplements
/// and the extraction prompt.
#[derive(Copy, Clone)]
struct Participant {
    role: Role,
    kind: crate::jobs::AgentKind,
    prompt_template: &'static str,
    angles_path: &'static str,
    extraction_prompt_path: &'static str,
}

/// Read-only code reviewers; the cohort size is calibrated per round.
const REVIEWER: Participant = Participant {
    role: Role::Reviewer,
    kind: crate::jobs::AgentKind::Reviewer,
    prompt_template: "review.md",
    angles_path: "review_angles.md",
    extraction_prompt_path: "extraction/reviewer.md",
};

/// The functional tester — the participant that may run the product.
const TESTER: Participant = Participant {
    role: Role::Qa,
    kind: crate::jobs::AgentKind::Tester,
    prompt_template: "qa.md",
    angles_path: "qa_angles.md",
    extraction_prompt_path: "extraction/qa.md",
};

impl Participant {
    /// The round's work prompt with the engineer's response and the project-commands brief
    /// baked in.
    fn prompt(self, ticket: &Ticket, commands_brief: &str) -> String {
        substitute(
            &load_prompt(self.prompt_template),
            &[
                ("{{agent_response}}", engineer_response(ticket)),
                ("{{project_commands}}", commands_brief),
            ],
        )
    }

    fn angles(self) -> Vec<String> {
        load_prompt_sections(self.angles_path)
    }

    fn extraction_prompt(self) -> String {
        load_prompt(self.extraction_prompt_path)
    }
}

/// The latest engineer comment on the ticket — the change under verification.
fn engineer_response(ticket: &Ticket) -> &str {
    ticket
        .comments
        .iter()
        .rev()
        .find(|c| c.role == Role::Engineer.as_str())
        .map_or("(no output)", |c| c.content.as_str())
}

/// Run the verification stage on a phase job: build a fresh round, or resume the round
/// recorded in the job's roster.
pub(crate) async fn run(ticket: Arc<Ticket>, ws: Workspace, job_id: String) {
    if guard_job_phase(&ticket.id, ACTIVE_PHASE, &job_id).await {
        return;
    }
    // The round's project-commands part — the workspace's discovered diagnostics commands — read
    // once before anything is dispatched: not being able to read them is a technical failure (no
    // bounce budget) — the round cannot be judged without knowing them.
    let commands = match crate::workspace::store().get_diagnostics(&ws.name).await {
        Ok(Some(cmds)) if !cmds.is_empty() => Some(cmds),
        Ok(_) => None,
        Err(e) => {
            warn!(
                ticket = %ticket.id,
                error = %e,
                "Failed to load the workspace's project commands — resetting for a fresh attempt",
            );
            reset_phase_attempt(
                &ticket,
                ACTIVE_PHASE,
                &job_id,
                "project commands load failure",
            )
            .await;
            return;
        }
    };
    let Some(roster) = read_roster_or_bail(&ticket.id, &job_id).await else {
        return;
    };
    let resume = !roster.is_empty();
    let plan = if resume {
        let Some(plan) = RoundPlan::from_roster(&roster) else {
            // A roster that is not a verification round is never resumed as one: dropping the
            // job makes the poller re-create it, so the round re-runs whole — with the
            // functional check in it. The job is gone afterwards, so the roster's shape is
            // logged here or not at all.
            warn!(
                ticket = %ticket.id,
                job = %job_id,
                rows = roster.len(),
                kinds = %roster.iter().map(|r| r.kind.as_str()).collect::<Vec<_>>().join(","),
                "Unreadable verification roster — re-driving the round from scratch",
            );
            let _ = crate::jobs::terminalize_job(&crate::session::store().conn, &job_id).await;
            return;
        };
        plan
    } else {
        // A roster write failure leaves the job with no plan: bail and let the poller re-drive
        // it rather than run a round the resume path could misread.
        let Some(plan) = RoundPlan::fresh(&ticket, &ws, &job_id, commands.as_ref()).await else {
            return;
        };
        plan
    };
    execute_round(ticket, ws, job_id, plan, resume, commands).await;
}

/// One verification round's dispatch plan: the two participant cohorts. The reviewer cohort
/// is empty exactly when this round skipped the code review.
struct RoundPlan {
    reviewers: Vec<AgentSlot>,
    testers: Vec<AgentSlot>,
}

impl RoundPlan {
    /// Build a fresh round: calibrated reviewers (none when the content is already checked)
    /// plus the always-present functional tester.
    async fn fresh(
        ticket: &Ticket,
        ws: &Workspace,
        job_id: &str,
        commands: Option<&DiagnosticsCommands>,
    ) -> Option<Self> {
        let commands_brief = commands_brief(commands);
        let tester_prompt = TESTER.prompt(ticket, &commands_brief);
        // The identical-content rule: content already checked on this ticket is not checked
        // again. It never skips the tester.
        let (reviewer_prompt, reviewers) = if skip_review_for_identical_content(ticket, ws).await {
            info!(
                ticket = %ticket.id,
                "Content identical to the reviewed base — dispatching the functional tester only",
            );
            (None, Vec::new())
        } else {
            let prompt = REVIEWER.prompt(ticket, &commands_brief);
            let slots = build_agent_slots(
                &ticket.id,
                REVIEWER.role,
                &prompt,
                &REVIEWER.angles(),
                0,
                compute_reviewer_count(ticket, ws.as_path()).await,
            );
            (Some(prompt), slots)
        };
        // The tester's idx continues the reviewer pack — its value only orders the roster and
        // the Running Agents view; cohorts are identified by roster kind.
        let testers = build_agent_slots(
            &ticket.id,
            TESTER.role,
            &tester_prompt,
            &TESTER.angles(),
            i64::try_from(reviewers.len()).unwrap_or(i64::MAX),
            TESTER_COUNT,
        );

        // The job's stored task records the round's primary work prompt (the phase dispatch
        // re-derives prompts from live state).
        let task = reviewer_prompt.as_deref().unwrap_or(&tester_prompt);
        sync_phase_job_task(&crate::session::store().conn, &ticket.id, job_id, task).await;
        let cohorts = [
            (REVIEWER.kind, reviewers.as_slice()),
            (TESTER.kind, testers.as_slice()),
        ];
        if let Err(e) = insert_round_slots(job_id, &cohorts).await {
            warn!(
                ticket = %ticket.id,
                job = %job_id,
                error = %e,
                "Failed to write the verification roster — round not started",
            );
            return None;
        }
        Some(Self { reviewers, testers })
    }

    /// Split a stored roster back into its cohorts by the rows' own roster labels — never by
    /// slot order, so a slot appended mid-round cannot be read as the functional tester.
    ///
    /// `None` for a roster this stage cannot read back as a round: it must carry exactly
    /// [`TESTER_COUNT`] tester row (any number of reviewer rows — a resumed round replays the
    /// cohort it wrote) and no row of any other kind. Such a roster is never resumed.
    fn from_roster(roster: &[crate::jobs::AgentRow]) -> Option<Self> {
        let cohort = |kind: crate::jobs::AgentKind| -> Vec<AgentSlot> {
            roster
                .iter()
                .filter(|row| row.kind == kind.as_str())
                .map(agent_slot_from_roster_row)
                .collect()
        };
        let reviewers = cohort(REVIEWER.kind);
        let testers = cohort(TESTER.kind);
        if testers.len() != TESTER_COUNT || reviewers.len() + testers.len() != roster.len() {
            return None;
        }
        Some(Self { reviewers, testers })
    }
}

/// The content identity the code reviewers were handed: `HEAD` plus the working tree's
/// identity ([`crate::git::commands::run_git_worktree_identity`]), read before the round's
/// cohorts run.
///
/// On a resumed round that reading happens at resume time, after whatever the interrupted
/// attempt left in the tree: what the pre-round reading removes from "already reviewed" is a
/// participant's own work, not an interrupted run's leftovers.
struct ReviewBase {
    head: String,
    tree: String,
}

/// The round's project-commands brief for the participants' work prompts: the concrete list of
/// commands this round runs, in execution order, or the notice that it runs none.
fn commands_brief(commands: Option<&DiagnosticsCommands>) -> String {
    let Some(commands) = commands else {
        return load_prompt("pipeline/round_commands_none.md");
    };
    let list = commands
        .commands()
        .iter()
        .filter_map(|(label, cmd)| cmd.map(|cmd| format!("- {label}: `{cmd}`")))
        .collect::<Vec<_>>()
        .join("\n");
    substitute(
        &load_prompt("pipeline/round_commands.md"),
        &[("{{commands}}", &list)],
    )
}

/// The round's project-commands result: the comment section that records the run (commands in
/// execution order, what passed, and the command it stopped at) and whether every command
/// passed — vacuously true with no commands configured.
struct ProjectCommands {
    section: String,
    passed: bool,
}

/// Run the workspace's project commands — in order, stopping at the first failure — on the
/// phase body's own task, concurrently with the participant cohorts. A command that changes the
/// tree (an auto-formatter, a lint auto-fix) is part of the round like a check: no command is
/// held back to a step of its own.
///
/// The judgement is the exit status and nothing else (`Some(0)` is a pass): what a command
/// writes is kept for the comment, but never read to decide the outcome. A failing command's
/// output is read unfiltered, so the record is built from what the tool printed rather than from
/// a profile's positional reading of it — [`failed_command_record`] keeps the lines that name
/// the failure, wherever they are in the output.
async fn run_project_commands(
    commands: Option<&DiagnosticsCommands>,
    ws: &Workspace,
) -> ProjectCommands {
    let Some(commands) = commands else {
        return ProjectCommands {
            section: format!(
                "**Project commands**\n\n{}",
                load_prompt("pipeline/commands_none.md")
            ),
            passed: true,
        };
    };

    let mut lines = String::new();
    let mut failed_at: Option<&str> = None;
    for (label, cmd) in commands.commands() {
        let Some(cmd) = cmd else {
            continue;
        };
        let started = std::time::Instant::now();
        let elapsed = || started.elapsed().as_secs_f64();
        match ShellTool::new(ShellMode::Full)
            .unfiltered()
            .execute_with_status(ws, serde_json::json!({ "command": cmd }))
            .await
        {
            Ok((_output, Some(0))) => {
                let _ = writeln!(lines, "- {label} (`{cmd}`): PASSED in {:.1}s", elapsed());
            }
            Ok((output, exit_code)) => {
                failed_command_record(&mut lines, label, cmd, elapsed(), exit_code, &output);
                failed_at = Some(label);
                break;
            }
            // A timeout or a launch failure: the tool's message carries the captured output's
            // failure-identifying excerpt, so the record needs no second cut here.
            Err(e) => {
                write_failed(&mut lines, label, cmd, elapsed(), "", &e.to_string());
                failed_at = Some(label);
                break;
            }
        }
    }

    let footer = match failed_at {
        Some(label) => format!("{} `{label}`", load_prompt("pipeline/commands_failed.md")),
        None => load_prompt("pipeline/commands_passed.md"),
    };
    ProjectCommands {
        section: format!("**Project commands**\n\n{}\n\n{footer}", lines.trim_end()),
        passed: failed_at.is_none(),
    }
}

/// The record of one failed command: the exit status on the command's own line, then the part of
/// its output that names the failure — inline, because the record must stand on its own. Output
/// that fits is recorded whole; a longer one is cut to the lines that identify the failure and
/// their context ([`failure_excerpt`]), never to a positional head and tail that a middle failure
/// would not survive.
fn failed_command_record(
    lines: &mut String,
    label: &str,
    cmd: &str,
    elapsed: f64,
    exit_code: Option<i32>,
    output: &str,
) {
    let suffix = exit_code.map_or_else(
        || " (terminated by signal)".to_string(),
        |code| format!(" (exit status {code})"),
    );
    let output = output.trim();
    let display = if output.is_empty() {
        "(no output)".to_string()
    } else {
        failure_excerpt(output)
    };
    write_failed(lines, label, cmd, elapsed, &suffix, &display);
}

/// Write a failed command's record: its verdict line (`suffix` naming how it failed), then its text
/// in a fence no run of backticks in that text can close.
fn write_failed(
    lines: &mut String,
    label: &str,
    cmd: &str,
    elapsed: f64,
    suffix: &str,
    text: &str,
) {
    let longest = text.split(|c| c != '`').map(str::len).max().unwrap_or(0);
    let fence = "`".repeat((longest + 1).max(3));
    let _ = writeln!(
        lines,
        "- {label} (`{cmd}`): FAILED in {elapsed:.1}s{suffix}\n\n{fence}\n{text}\n{fence}"
    );
}

async fn execute_round(
    ticket: Arc<Ticket>,
    ws: Workspace,
    job_id: String,
    plan: RoundPlan,
    resume: bool,
    commands: Option<DiagnosticsCommands>,
) {
    if resume {
        // A fresh round's slots are already launched — re-arming them would only stamp the
        // job's updated time again.
        let cohorts: [&[AgentSlot]; 2] = [&plan.reviewers, &plan.testers];
        rearm_resumed_slots(&ticket.id, &job_id, "verification", &cohorts).await;
    }
    let reviewer_count = plan.reviewers.len();
    let tester_count = plan.testers.len();
    let command_count = commands.as_ref().map_or(0, |c| {
        c.commands().iter().filter(|(_, cmd)| cmd.is_some()).count()
    });
    info!(
        ticket = %ticket.id,
        "Dispatching {reviewer_count} reviewer(s), {tester_count} tester(s) and {command_count} project command(s) in parallel",
    );

    // A skipped code review was never dispatched, so it has neither content to record nor slots
    // to write. The identity is read BEFORE either cohort runs: the round records the content
    // the code review was handed, never the tree the tester may have touched by the end. A
    // repository the reading cannot read leaves no base, which only means a later round reviews
    // the same content again.
    let review_skipped = plan.reviewers.is_empty();
    let base = if review_skipped {
        None
    } else {
        match crate::git::commands::run_git_worktree_identity(ws.as_path()).await {
            Ok((head, tree)) => Some(ReviewBase { head, tree }),
            Err(e) => {
                debug!(
                    ticket = %ticket.id,
                    error = %e,
                    "Could not read the reviewed content identity",
                );
                None
            }
        }
    };

    // One round: the two participant cohorts and the project commands, all started at the same
    // time on the same phase job.
    let ((reviewer_results, reviewer_paused), (tester_results, tester_paused), commands_outcome) = tokio::join!(
        dispatch_cohort(&ticket, &ws, REVIEWER, &plan.reviewers, &job_id, resume),
        dispatch_cohort(&ticket, &ws, TESTER, &plan.testers, &job_id, resume),
        run_project_commands(commands.as_ref(), &ws),
    );

    if guard_job_phase(&ticket.id, ACTIVE_PHASE, &job_id).await {
        return;
    }
    // A pause-freeze is NOT a technical failure: leave the job in place for the unpause
    // re-drive (the typed `paused` signal was captured at bail time, so it survives a
    // workspace-unpause race).
    if reviewer_paused || tester_paused {
        pause_freezing(&ticket, &job_id).await;
        return;
    }
    finalize_round(
        &ws,
        &ticket,
        &reviewer_results,
        &tester_results,
        base,
        review_skipped,
        &job_id,
        &commands_outcome,
    )
    .await;
}

/// Dispatch one participant cohort on the shared phase job. A cohort with no slot (a skipped
/// code review) loads nothing and dispatches nothing.
async fn dispatch_cohort(
    ticket: &Arc<Ticket>,
    ws: &Workspace,
    participant: Participant,
    slots: &[AgentSlot],
    job_id: &str,
    resume: bool,
) -> (Vec<ParallelVerdict>, bool) {
    if slots.is_empty() {
        return (Vec::new(), false);
    }
    run_parallel_agents(
        ticket,
        ws,
        participant.role,
        &participant.extraction_prompt(),
        ExtractionMode::ScoreVerdict,
        job_id,
        slots,
        ACTIVE_PHASE,
        resume,
    )
    .await
}

#[expect(clippy::too_many_arguments)]
async fn finalize_round(
    ws: &Workspace,
    ticket: &Ticket,
    reviewer_results: &[ParallelVerdict],
    tester_results: &[ParallelVerdict],
    base: Option<ReviewBase>,
    review_skipped: bool,
    job_id: &str,
    commands: &ProjectCommands,
) {
    let results: Vec<ParallelVerdict> = reviewer_results
        .iter()
        .chain(tester_results)
        .cloned()
        .collect();
    let transitioned =
        process_round_verdicts(ws, ticket, &results, review_skipped, job_id, commands).await;
    if !review_skipped {
        // Tied to the code review having actually run: the recording is what makes the
        // identical-content rule fire later, and the tester's verdict must never stand in for
        // it. A round that did not move the ticket on stages nothing.
        record_reviewed_base(ws, &ticket.id, base, transitioned).await;
    }
}

/// Process the merged round's verdicts: build the consolidated comment, decide pass/fail, and
/// update the ticket phase. Returns whether the outcome was applied.
async fn process_round_verdicts(
    ws: &Workspace,
    ticket: &Ticket,
    results: &[ParallelVerdict],
    review_skipped: bool,
    job_id: &str,
    commands: &ProjectCommands,
) -> bool {
    // Two failure classes: a participant that did NOT complete (NoResponse/ParseFailed) is a
    // HARD TECHNICAL failure — reset the attempt (delete job + pause; no bounce budget). One
    // that completed but found issues is a rework verdict — bounce to development, consuming
    // bounce budget.
    let technical_failure = results.iter().any(ParallelVerdict::is_technical_failure);
    // The rework condition is the participants' verdicts OR the project commands.
    let rework_failure = !technical_failure
        && (!commands.passed
            || results
                .iter()
                .any(|r| matches!(r, ParallelVerdict::Verdict(v) if !verdict_passes(v))));

    if crate::shutdown::aborting() {
        info!(
            ticket = %ticket.id,
            stage = STAGE,
            "Verification round cut short by drain — job stays launched for boot resume",
        );
        return false;
    }

    if technical_failure {
        // Hard technical failure: the whole merged round is destroyed and re-driven as a unit.
        warn!(
            ticket = %ticket.id,
            stage = STAGE,
            "Verification round hit a technical participant failure — resetting for a fresh attempt",
        );
        reset_phase_attempt(ticket, ACTIVE_PHASE, job_id, "verification failure").await;
        return false;
    }

    // The round's one artifact: the consolidated joint comment, carrying the
    // skipped-code-review note when the reviewers were not dispatched.
    let (round, outcome) = build_round_grouping(
        STAGE,
        results,
        // `stage_role(STAGE)` resolves the comment to the tester's badge and icon, so the
        // grouping pass runs and is labelled under the same role.
        Role::Qa,
        // A skipped code review leaves the tester as the round's only participant.
        review_skipped,
        ws,
        &ticket.id,
        &ticket.title,
    )
    .await;
    let mut verdict = render_joint_comment(
        &round,
        &outcome,
        &crate::consensus::ItemTable::new(&round.issues),
    );
    if review_skipped {
        verdict.insert_str(0, SKIPPED_REVIEW_NOTE);
    }
    // The round's ONE comment, both halves of its result: the project commands, then the
    // participants' verdict.
    let comment = format!("{}\n\n---\n\n{verdict}", commands.section);

    if !rework_failure {
        return apply_clean_round(ticket, &comment, job_id).await;
    }

    let outcome = bounce_to_development(
        ticket,
        ACTIVE_PHASE,
        STAGE,
        STAGE,
        ACTIVE_PHASE.as_ref(),
        &comment,
        job_id,
    )
    .await;
    matches!(outcome, FinalizeOutcome::Applied)
}

/// Apply the clean-pass outcome: write the joint comment, transition the ticket to sanitation,
/// and delete the phase job. `false` if the transition was not applied (phase moved concurrently).
async fn apply_clean_round(ticket: &Ticket, comment: &str, job_id: &str) -> bool {
    if !matches!(
        comment_and_transition(
            TransitionCtx::buffered(
                ticket,
                ACTIVE_PHASE,
                SUCCESS_PHASE,
                STAGE,
                ACTIVE_PHASE.as_ref(),
            ),
            STAGE,
            comment,
        )
        .await,
        FinalizeOutcome::Applied
    ) {
        return false;
    }
    info!(
        ticket = %ticket.id,
        "{STAGE}: the project commands and every participant passed (≥ {threshold}/10)",
        threshold = VERIFICATION_THRESHOLD,
    );
    // Delete the phase job; the puller creates the next phase job.
    let _ = crate::jobs::terminalize_job(&crate::session::store().conn, job_id).await;
    true
}

#[must_use]
fn verdict_passes(verdict: &crate::Verdict) -> bool {
    verdict.score >= VERIFICATION_THRESHOLD
}

/// Whether git is usable for the reviewer cohort; the tester never consults it.
async fn git_available(ws: &Workspace) -> bool {
    crate::git::commands::git_is_installed().await
        && crate::git::commands::is_git_repo(ws.as_path())
}

/// Whether this round's code review may be skipped: the working tree is identical to the
/// ticket's recorded reviewed base. A git failure is fail-open — the reviewers run.
async fn skip_review_for_identical_content(ticket: &Ticket, ws: &Workspace) -> bool {
    if !git_available(ws).await {
        return false;
    }
    match compute_review_skip(ticket, ws.as_path()).await {
        Ok(skip) => skip,
        Err(e) => {
            warn!(
                ticket = %ticket.id,
                error = %e,
                "Git status check failed for skip-review — dispatching the reviewers",
            );
            false
        }
    }
}

fn should_skip_review(
    reviewed_head: Option<&str>,
    reviewed_tree: Option<&str>,
    current_head: Option<&str>,
    current_tree: Option<&str>,
    porcelain: &str,
) -> bool {
    let (Some(base_head), Some(base_tree)) = (reviewed_head, reviewed_tree) else {
        return false;
    };
    let (Some(head), Some(tree)) = (current_head, current_tree) else {
        return false;
    };
    head == base_head && tree == base_tree && !has_unstaged_changes(porcelain)
}

async fn compute_review_skip(ticket: &Ticket, repo_path: &Path) -> anyhow::Result<bool> {
    let porcelain = run_git_status(repo_path).await?;
    let head = run_git_head(repo_path).await.ok();
    let tree = run_git_write_tree(repo_path).await.ok();
    if (head.is_none() || tree.is_none()) && ticket.reviewed_head.is_some() {
        warn!(
            ticket = %ticket.id,
            head = head.is_some(),
            tree = tree.is_some(),
            "Could not compute full content identity — dispatching the reviewers",
        );
    }
    Ok(should_skip_review(
        ticket.reviewed_head.as_deref(),
        ticket.reviewed_tree.as_deref(),
        head.as_deref(),
        tree.as_deref(),
        &porcelain,
    ))
}

async fn working_tree_churn(repo_path: &Path) -> anyhow::Result<i64> {
    let snapshot = run_git_worktree_snapshot(repo_path).await?;
    // An unborn HEAD is not a valid churn baseline: surface as Err so the caller defaults the
    // reviewer base instead of calibrating on a zero diff.
    if snapshot.unborn_head {
        anyhow::bail!("Repository has no commits — no churn baseline");
    }
    // Churn calibration uses only the exact line counts — the huge/binary untracked file-count
    // must NOT influence reviewer counts.
    Ok(snapshot.stats.added + snapshot.stats.removed)
}

/// Compute the reviewer count for a round: the working tree's churn calibrates the band (1 up
/// to [`crate::pipeline::verdict::DEFAULT_REVIEW_COUNT_LOW_CHURN`] changed lines, 2 below
/// [`crate::pipeline::verdict::DEFAULT_REVIEW_COUNT_HIGH_CHURN`], and
/// [`crate::pipeline::verdict::MAX_REVIEWERS`] from there), the priority floor applies on top,
/// and an unmeasurable churn falls back to the ceiling.
///
/// Every path stays between one reviewer and the ceiling — an empty cohort would be
/// indistinguishable from the identical-content skip, which the *plan* decides.
pub(crate) async fn compute_reviewer_count(ticket: &Ticket, repo_path: &Path) -> usize {
    use crate::pipeline::verdict::{
        DEFAULT_REVIEW_COUNT_HIGH_CHURN, DEFAULT_REVIEW_COUNT_LOW_CHURN, MAX_REVIEWERS,
        review_agent_count, review_base_from_signals,
    };

    let base = match working_tree_churn(repo_path).await {
        Ok(total) => {
            let base = review_base_from_signals(
                total,
                DEFAULT_REVIEW_COUNT_LOW_CHURN,
                DEFAULT_REVIEW_COUNT_HIGH_CHURN,
            );
            // Debug: per-round calibration bookkeeping; an uncomputable churn keeps its warn
            // record below.
            debug!(
                ticket = %ticket.id,
                total_churn = total,
                reviewer_base = base,
                "Reviewer count calibration: base {base} from total churn",
            );
            base
        }
        Err(e) => {
            warn!(
                ticket = %ticket.id,
                error = %e,
                "Could not compute working-tree churn — reviewer base defaults to the ceiling",
            );
            MAX_REVIEWERS
        }
    };
    review_agent_count(base, ticket.priority)
}

/// Write out the ticket's reviewed base from the identity read before the round ran — the
/// content the code reviewers were handed (see [`ReviewBase`]) — in a round whose code review
/// ran and whose outcome was applied (a pass OR a bounce, which is what makes the
/// identical-content rule fire on the round after a bounced one).
///
/// `git add -A` still runs here, and only here: it is not part of the recording, but the next
/// round's skip check reads HEAD and the *index* tree, so the index has to end a recorded round
/// holding the working tree. It runs under the round's own gate, so a round that never moves on
/// leaves the index and the working tree as it found them.
async fn record_reviewed_base(
    ws: &Workspace,
    ticket_id: &str,
    base: Option<ReviewBase>,
    transitioned: bool,
) {
    if !transitioned || !git_available(ws).await {
        return;
    }
    // Staged first, recorded second: a round that moves the ticket on leaves the index holding
    // the working tree exactly as before, whether or not the identity could be read.
    if let Err(e) = run_git_add_all(ws.as_path()).await {
        warn!(
            ticket = %ticket_id,
            error = %e,
            "Failed to stage changes after review — reviewed base not recorded",
        );
        return;
    }
    let Some(base) = base else {
        warn!(
            ticket = %ticket_id,
            "Could not read the reviewed content before the round — reviewed base not recorded",
        );
        return;
    };
    if let Err(e) = super::board()
        .set_reviewed_base(ticket_id, Some(&base.head), Some(&base.tree))
        .await
    {
        warn!(
            ticket = %ticket_id,
            error = %e,
            "Failed to record reviewed base — later rounds will re-review",
        );
    } else {
        debug!(ticket = %ticket_id, "Recorded reviewed base after verification");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::git::commands::MAX_UNTRACKED_SIZE;
    use crate::util::test::init_temp_repo;

    #[tokio::test]
    async fn working_tree_churn_ignores_huge_binary_file_count() {
        let (_dir, repo_path) = init_temp_repo();
        std::fs::write(repo_path.join("a.rs"), b"fn foo() {\n    bar();\n}\n").unwrap();
        let size = usize::try_from(MAX_UNTRACKED_SIZE).unwrap() + 1;
        std::fs::write(repo_path.join("big.bin"), vec![b'a'; size]).unwrap();
        let churn = working_tree_churn(&repo_path).await.unwrap();
        // Churn is added+removed only; the oversized file contributes nothing.
        assert_eq!(churn, 3);
    }

    /// A command that fails after ~28 KB of noise: the record must carry the lines that name the
    /// failure, wherever they are, and never the positional head a volume-based cut would show.
    #[cfg(unix)]
    #[tokio::test]
    async fn a_failing_project_command_records_the_lines_that_name_the_failure() {
        let dir = tempfile::TempDir::new().unwrap();
        let ws = crate::workspace::test_ws(dir.path());
        std::fs::write(
            dir.path().join("fail.sh"),
            "for i in $(seq 1 800); do echo \"checking item $i of the suite\"; done\n\
             echo '---- test_add stdout ----'\n\
             echo \"thread 'test_add' panicked at src/lib.rs:10:5:\"\n\
             echo 'assertion `left == right` failed'\n\
             for i in $(seq 1 400); do echo \"cleanup note $i\"; done\n\
             exit 101\n",
        )
        .unwrap();
        let commands = DiagnosticsCommands {
            unit_test: Some("sh fail.sh".to_string()),
            ..Default::default()
        };

        let record = run_project_commands(Some(&commands), &ws).await;

        assert!(!record.passed);
        assert!(
            record.section.contains("(exit status 101)"),
            "{}",
            record.section
        );
        for needle in [
            "---- test_add stdout ----",
            "thread 'test_add' panicked at src/lib.rs:10:5:",
            "assertion `left == right` failed",
        ] {
            assert!(
                record.section.contains(needle),
                "the record lacks {needle:?}:\n{}",
                record.section
            );
        }
        assert!(
            !record.section.contains("checking item 1 of the suite"),
            "the record must not spend its room on the output's head:\n{}",
            record.section
        );
        assert!(
            record.section.contains("lines omitted"),
            "a partial record says so:\n{}",
            record.section
        );
    }

    /// A passing command's record is exactly what it always was: its own line, nothing of its
    /// output.
    #[cfg(unix)]
    #[tokio::test]
    async fn a_passing_project_command_records_only_its_verdict() {
        let dir = tempfile::TempDir::new().unwrap();
        let ws = crate::workspace::test_ws(dir.path());
        std::fs::write(
            dir.path().join("pass.sh"),
            "echo passing-output-marker\nexit 0\n",
        )
        .unwrap();
        let commands = DiagnosticsCommands {
            format: Some("sh pass.sh".to_string()),
            ..Default::default()
        };

        let record = run_project_commands(Some(&commands), &ws).await;

        assert!(record.passed);
        assert!(
            record.section.contains("format (`sh pass.sh`): PASSED in "),
            "{}",
            record.section
        );
        assert!(
            !record.section.contains("passing-output-marker"),
            "{}",
            record.section
        );
    }

    /// A command's own output cannot end the record's block: the fence grows past its backticks.
    #[cfg(unix)]
    #[tokio::test]
    async fn a_record_fences_output_that_holds_backticks() {
        let dir = tempfile::TempDir::new().unwrap();
        let ws = crate::workspace::test_ws(dir.path());
        std::fs::write(
            dir.path().join("fence.sh"),
            "printf 'error: a block\\n```\\ninside the output\\n```\\n'\nexit 1\n",
        )
        .unwrap();
        let commands = DiagnosticsCommands {
            unit_test: Some("sh fence.sh".to_string()),
            ..Default::default()
        };

        let record = run_project_commands(Some(&commands), &ws).await;

        assert!(record.section.contains("````"), "{}", record.section);
        assert!(
            record.section.contains("inside the output"),
            "{}",
            record.section
        );
    }

    /// A command that printed nothing still records how it failed.
    #[cfg(unix)]
    #[tokio::test]
    async fn a_silent_failure_records_its_exit_status() {
        let dir = tempfile::TempDir::new().unwrap();
        let ws = crate::workspace::test_ws(dir.path());
        let commands = DiagnosticsCommands {
            unit_test: Some("exit 1".to_string()),
            ..Default::default()
        };

        let record = run_project_commands(Some(&commands), &ws).await;

        assert!(!record.passed);
        assert!(
            record.section.contains("(exit status 1)"),
            "{}",
            record.section
        );
        assert!(record.section.contains("(no output)"), "{}", record.section);
    }
}