hird 0.3.1

Cross-harness agent work queue and shared assertion memory, backed by one local SQLite database
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
//! The verdict: a review that closes its own loop.
//!
//! Recusal (see `recusal.rs`) got the work in front of a second harness. What
//! it did not do is listen to the answer. A review ended in prose — a `result`
//! line saying, somewhere in its own words, whether the work was any good —
//! and then the loop dangled: a human had to read the review, decide that
//! "the error path drops the lock" means *broken*, find the task it reviewed,
//! and reopen it by hand, carrying the findings across themselves. Every
//! other hand-off in hird files itself, on the observation that something an
//! agent — or a human — has to remember to do is something that does not
//! happen. The one hand-off left by hand was the one carrying the judgment.
//!
//! So a review now ends in a **verdict**, and the verdict is enforced where
//! the review ends: completing a task that is a review requires one, and the
//! two possible verdicts name their own consequences. `upheld` means the work
//! stands — its card can say so, on the word of a harness that recusal
//! guarantees did not write it. `sent_back` means it does not stand, and the
//! queue acts immediately: the work is reopened with the reviewer's findings
//! appended to its brief, so the next agent to claim it — its author included
//! — is handed exactly what must change without having to know to ask. The
//! work was filed with `review` set, so finishing it again files a fresh
//! review, and the loop runs, round after round, until a review upholds. No
//! agent is the last word on its own work; no human is the courier between
//! agents.
//!
//! One invariant bends, knowingly. "Terminal statuses only leave via a human
//! reopen" was written when nothing but a human could be trusted to judge
//! finished work. The recusal edge is what changed that: a `sent_back` comes
//! from a harness the queue *proves* did not do the work, which is precisely
//! the trust the human reopen was standing in for. The human keeps the last
//! word they always had — cancel the task, lift the recusal, ignore the
//! round — but they stop being the loop's transport.
//!
//! And because every verdict is delivered on the record — who judged, whose
//! work, which round — the queue accumulates the one measurement it is
//! uniquely placed to take: whose work survives a reading by a different
//! model. `hird agents --record` is that table. It is a report, not a scheduler;
//! nothing routes work by it. Reading it is the human's job, and deciding
//! what to do about a harness that ships rework is exactly the kind of call
//! hird leaves to people.

use std::collections::BTreeMap;

use rusqlite::{params, Connection, Transaction};

use crate::error::{Error, Result};
use crate::identity::{actor_harness, Axis};
use crate::model::{now_ts, EventKind, RecordRow, Status, Verdict, VerdictRecord};

/// One verdict as it landed on a reviewed task, and what the queue did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Delivered {
    /// The work task judged.
    pub seq: i64,
    pub verdict: Verdict,
    /// True when `sent_back` found the work `done` and returned it to the
    /// pool. False for `upheld`, and for work a human had already moved.
    pub reopened: bool,
}

impl Delivered {
    /// One sentence, aimed at a model that has to relay it to a human.
    pub fn describe(&self) -> String {
        match (self.verdict, self.reopened) {
            (Verdict::Upheld, _) => format!("task {} upheld — the work stands", self.seq),
            (Verdict::SentBack, true) => format!(
                "task {} sent back — open again with your findings appended to its brief",
                self.seq
            ),
            (Verdict::SentBack, false) => format!(
                "task {} sent back, but it had already been moved; the verdict is on record",
                self.seq
            ),
        }
    }
}

/// Repository over `task_verdicts`.
pub struct Verdicts<'a> {
    conn: &'a Connection,
}

impl<'a> Verdicts<'a> {
    pub(crate) fn new(conn: &'a Connection) -> Verdicts<'a> {
        Verdicts { conn }
    }

    /// Every verdict delivered on task `seq`'s work, oldest first.
    ///
    /// More than one means rounds: sent back, redone, judged again.
    pub fn for_task(&self, seq: i64) -> Result<Vec<VerdictRecord>> {
        self.query("WHERE t.seq = ?1 ORDER BY v.at ASC, v.id ASC", params![seq])
    }

    /// The newest verdict on task `seq`'s work, if any has been delivered.
    pub fn latest(&self, seq: i64) -> Result<Option<VerdictRecord>> {
        Ok(self.for_task(seq)?.pop())
    }

    /// The verdicts review `seq` delivered, one per task it reviewed.
    pub fn of_review(&self, seq: i64) -> Result<Vec<VerdictRecord>> {
        self.query("WHERE r.seq = ?1 ORDER BY v.at ASC, v.id ASC", params![seq])
    }

    /// The newest verdict per work task in `scope`, for annotating a board.
    pub fn standing(&self, scope: &super::ProjectScope) -> Result<BTreeMap<i64, Verdict>> {
        let (clause, value) = scope.clause("t.project");
        let sql = format!(
            "SELECT t.seq, v.verdict FROM task_verdicts v
             JOIN tasks t ON t.id = v.task_id
             WHERE {clause} ORDER BY v.at ASC, v.id ASC"
        );
        let mut stmt = self.conn.prepare(&sql)?;
        let binds: Vec<&str> = value.into_iter().collect();
        let rows = stmt.query_map(rusqlite::params_from_iter(binds), |row| {
            Ok((row.get::<_, i64>(0)?, row.get::<_, String>(1)?))
        })?;
        let mut out = BTreeMap::new();
        for row in rows {
            let (seq, verdict) = row?;
            if let Ok(verdict) = verdict.parse::<Verdict>() {
                // Later rows overwrite earlier ones: the newest verdict stands.
                out.insert(seq, verdict);
            }
        }
        Ok(out)
    }

    /// Each harness's — or each person's — standing in the record: verdicts
    /// received on its work, first-pass rate across distinct tasks, and
    /// verdicts it has handed out.
    ///
    /// Aggregated from the delivered verdicts and nothing else, so it holds
    /// still when leases churn and workers hand tasks on: a verdict names the
    /// worker it judged at the moment it landed, and that name does not move.
    ///
    /// `axis` picks which half of those names to group by. Along
    /// [`Axis::Person`], verdicts whose actors name nobody are left out
    /// entirely — an unattributed actor has no person to credit, and guessing
    /// one would be the record steering rather than measuring.
    pub fn record(&self, scope: &super::ProjectScope, axis: Axis) -> Result<Vec<RecordRow>> {
        let (clause, value) = scope.clause("t.project");
        let sql = format!(
            "SELECT v.worker, v.reviewer, v.verdict, v.task_id FROM task_verdicts v
             JOIN tasks t ON t.id = v.task_id
             WHERE {clause} ORDER BY v.at ASC, v.id ASC"
        );
        let mut stmt = self.conn.prepare(&sql)?;
        let binds: Vec<&str> = value.into_iter().collect();
        let rows = stmt.query_map(rusqlite::params_from_iter(binds), |row| {
            Ok((
                row.get::<_, String>(0)?,
                row.get::<_, String>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, String>(3)?,
            ))
        })?;

        fn entry<'m>(records: &'m mut BTreeMap<String, RecordRow>, who: &str) -> &'m mut RecordRow {
            records.entry(who.to_string()).or_insert_with(|| RecordRow {
                who: who.to_string(),
                ..RecordRow::default()
            })
        }
        let mut records: BTreeMap<String, RecordRow> = BTreeMap::new();
        let mut first_seen: BTreeMap<String, (String, Verdict)> = BTreeMap::new();
        for row in rows {
            let (worker, reviewer, verdict, task_id) = row?;
            let Ok(verdict) = verdict.parse::<Verdict>() else {
                continue;
            };
            if let Some(who) = axis.key(&worker).filter(|_| !worker.is_empty()) {
                let who = who.to_string();
                let rec = entry(&mut records, &who);
                rec.judged += 1;
                match verdict {
                    Verdict::Upheld => rec.upheld += 1,
                    Verdict::SentBack => rec.sent_back += 1,
                }
                // The first verdict on a task is the one that measures the
                // work as delivered, before any round of rework.
                first_seen.entry(task_id).or_insert((who, verdict));
            }
            if let Some(who) = axis.key(&reviewer) {
                let rec = entry(&mut records, who);
                match verdict {
                    Verdict::Upheld => rec.upheld_given += 1,
                    Verdict::SentBack => rec.sent_back_given += 1,
                }
            }
        }
        for (who, verdict) in first_seen.into_values() {
            if let Some(rec) = records.get_mut(&who) {
                rec.tasks_judged += 1;
                if verdict == Verdict::Upheld {
                    rec.first_pass += 1;
                }
            }
        }
        Ok(records.into_values().collect())
    }

    fn query(&self, tail: &str, binds: impl rusqlite::Params) -> Result<Vec<VerdictRecord>> {
        let sql = format!(
            "SELECT r.seq, t.seq, v.verdict, v.worker, v.reviewer, v.at
             FROM task_verdicts v
             JOIN tasks r ON r.id = v.review_id
             JOIN tasks t ON t.id = v.task_id
             {tail}"
        );
        let mut stmt = self.conn.prepare(&sql)?;
        let rows = stmt.query_map(binds, |row| {
            Ok((
                row.get::<_, i64>(0)?,
                row.get::<_, i64>(1)?,
                row.get::<_, String>(2)?,
                row.get::<_, String>(3)?,
                row.get::<_, String>(4)?,
                row.get::<_, String>(5)?,
            ))
        })?;
        let mut out = Vec::new();
        for row in rows {
            let (review_seq, task_seq, verdict, worker, reviewer, at) = row?;
            let Ok(verdict) = verdict.parse::<Verdict>() else {
                continue;
            };
            out.push(VerdictRecord {
                review_seq,
                task_seq,
                verdict,
                worker,
                reviewer,
                at,
            });
        }
        Ok(out)
    }
}

/// Deliver the verdict a completing review carries, or refuse the completion.
///
/// Runs inside the same transaction as the completion itself, so a refusal
/// leaves no half-finished review and a delivered verdict cannot land without
/// its review going `done`. Being a review is a fact about the task — it has
/// recusal edges — not about the caller: a task with edges must carry a
/// verdict, a task without must not, and both refusals say exactly what to do
/// instead, because the agent reading them is mid-completion and has nobody
/// to ask.
pub(crate) fn deliver_in_tx(
    tx: &Transaction<'_>,
    review: &crate::model::Task,
    verdict: Option<Verdict>,
    actor: &str,
    result: &str,
    rejection: super::attempts::Outcome,
    escalate_after: u32,
) -> Result<Vec<Delivered>> {
    let reviewed = reviewed_tasks(tx, &review.id)?;
    let Some(verdict) = verdict else {
        if reviewed.is_empty() {
            return Ok(Vec::new());
        }
        let seqs = reviewed
            .iter()
            .map(|(seq, ..)| format!("{seq}"))
            .collect::<Vec<_>>()
            .join(", ");
        return Err(Error::invalid(format!(
            "task {} is a review of task {seqs}: complete it with a verdict — \"upheld\" if \
             the work stands, or \"sent_back\" to return it to the pool carrying your findings",
            review.seq
        )));
    };
    if reviewed.is_empty() {
        return Err(Error::invalid(format!(
            "task {} is not a review of anything; complete it without a verdict",
            review.seq
        )));
    }

    let now = now_ts();
    let mut delivered = Vec::with_capacity(reviewed.len());
    for (work_seq, work_id, work_status) in reviewed {
        // The name on the verdict is whoever held the work when it landed,
        // read from the trail the way recusal reads it — so the record stays
        // true even after the task is reopened and picked up by someone else.
        let worker = super::recusal::worker_of(tx, &work_id)?.unwrap_or_default();
        tx.execute(
            "INSERT INTO task_verdicts (id, review_id, task_id, verdict, worker, reviewer, at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            params![
                super::new_id(),
                review.id,
                work_id,
                verdict.as_str(),
                worker,
                actor,
                now
            ],
        )?;
        super::tasks::insert_event(
            tx,
            &review.id,
            &now,
            actor,
            EventKind::Reviewed,
            &format!("verdict on task {work_seq}: {verdict}"),
        )?;

        let reopened = match verdict {
            Verdict::Upheld => {
                super::tasks::insert_event(
                    tx,
                    &work_id,
                    &now,
                    actor,
                    EventKind::Reviewed,
                    &format!("upheld by review {}", review.seq),
                )?;
                false
            }
            Verdict::SentBack if work_status == Status::Done => {
                // The findings travel in the brief, not in a comment thread:
                // whoever claims the reopened task is handed them the same way
                // it is handed everything else, without knowing to ask.
                let findings = format!(
                    "\n\n---\n\nSent back by review {} ({}):\n\n{}\n",
                    review.seq,
                    actor_harness(actor),
                    result.trim()
                );
                tx.execute(
                    "UPDATE tasks
                     SET status = 'open', claimed_by = NULL, lease_expires_at = NULL,
                         result = NULL, body = body || ?2, updated_at = ?3
                     WHERE id = ?1",
                    params![work_id, findings, now],
                )?;
                super::tasks::insert_move(
                    tx,
                    &work_id,
                    &now,
                    actor,
                    EventKind::Reopened,
                    &format!(
                        "sent back by review {}: {}",
                        review.seq,
                        crate::fmt::truncate(result.trim(), 120)
                    ),
                    Status::Done.as_str(),
                    Status::Open,
                )?;
                // The work's dependents were let through on the strength of a
                // `done` that has just been taken back. Any of them being
                // worked right now is building on ground that moved, and its
                // holder is the one participant with no way to notice — so
                // the fallout goes on each of their trails here, in the same
                // transaction as the reopen, and their next check-in relays
                // it (`Deps::shifted`).
                notify_live_dependents(tx, &work_id, work_seq, review.seq, actor, &now)?;
                // Counted where it is decided, so the round number and any
                // escalation land in the same transaction as the reopen.
                super::attempts::record_in(
                    tx,
                    &work_id,
                    &worker,
                    rejection,
                    &format!("review {}", review.seq),
                    actor,
                    result,
                    escalate_after,
                )?;
                true
            }
            Verdict::SentBack => {
                // A human already moved the work — reopened it themselves,
                // cancelled it, or it failed since. The verdict still lands on
                // the record; the queue does not overrule a human's move.
                super::tasks::insert_event(
                    tx,
                    &work_id,
                    &now,
                    actor,
                    EventKind::Reviewed,
                    &format!(
                        "sent back by review {}, but the work is {work_status}; left as it is",
                        review.seq
                    ),
                )?;
                false
            }
        };
        delivered.push(Delivered {
            seq: work_seq,
            verdict,
            reopened,
        });
    }
    Ok(delivered)
}

/// Put the sent-back on the trail of every live dependent of the reopened
/// work, so the shift is on the record the moment it happens rather than
/// whenever somebody thinks to look.
fn notify_live_dependents(
    tx: &Transaction<'_>,
    work_id: &str,
    work_seq: i64,
    review_seq: i64,
    actor: &str,
    now: &str,
) -> Result<()> {
    let dependents: Vec<String> = {
        let mut stmt = tx.prepare(
            "SELECT t.id FROM task_deps d
             JOIN tasks t ON t.id = d.task_id
             WHERE d.depends_on_id = ?1 AND t.status IN ('claimed','in_progress')
             ORDER BY t.seq",
        )?;
        let rows = stmt.query_map([work_id], |row| row.get(0))?;
        rows.collect::<rusqlite::Result<Vec<_>>>()?
    };
    for dependent in dependents {
        super::tasks::insert_event(
            tx,
            &dependent,
            now,
            actor,
            EventKind::GroundShifted,
            &format!(
                "task {work_seq}, which this task builds on, was sent back by \
                 review {review_seq} and reopened"
            ),
        )?;
    }
    Ok(())
}

/// The tasks a review judges: everything it is recused from.
fn reviewed_tasks(conn: &Connection, review_id: &str) -> Result<Vec<(i64, String, Status)>> {
    let mut stmt = conn.prepare(
        "SELECT t.seq, t.id, t.status FROM task_recusals r
         JOIN tasks t ON t.id = r.from_task_id
         WHERE r.task_id = ?1 ORDER BY t.seq",
    )?;
    let rows = stmt.query_map([review_id], |row| {
        Ok((
            row.get::<_, i64>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, String>(2)?,
        ))
    })?;
    let mut out = Vec::new();
    for row in rows {
        let (seq, id, status) = row?;
        let status = status
            .parse::<Status>()
            .map_err(|e| Error::invalid(e.to_string()))?;
        out.push((seq, id, status));
    }
    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::Db;
    use crate::repo::ProjectScope;
    use std::time::Duration;

    const PROJECT: &str = "/tmp/project";
    const TTL: Duration = Duration::from_secs(900);

    fn db() -> Db {
        Db::open_in_memory().unwrap()
    }

    /// File work marked for review, complete it as `worker`, and return
    /// `(work, review)` seqs.
    fn reviewed_work(db: &Db, worker: &str) -> (i64, i64) {
        let seq = db
            .tasks()
            .create(PROJECT, "Port the loader", "keep the precedence", 0, "cli")
            .unwrap()
            .seq;
        db.tasks().set_review(seq, true, "cli").unwrap();
        db.scopes()
            .declare(
                seq,
                &["src/loader.rs".to_string()],
                "cli",
                super::super::OnConflict::Report,
            )
            .unwrap();
        db.tasks().claim(seq, worker, TTL).unwrap();
        let finished = db.tasks().complete(seq, worker, "ported it").unwrap();
        (seq, finished.review.expect("a review was filed"))
    }

    #[test]
    fn completing_a_review_without_a_verdict_is_refused_and_the_refusal_teaches() {
        let db = db();
        let (work, review) = reviewed_work(&db, "codex:9f2c");
        db.tasks().claim(review, "claude-code:af31", TTL).unwrap();

        let err = db
            .tasks()
            .complete(review, "claude-code:af31", "looks wrong")
            .unwrap_err()
            .to_string();
        assert!(err.contains(&format!("review of task {work}")), "{err}");
        assert!(err.contains("upheld"), "{err}");
        assert!(err.contains("sent_back"), "{err}");
        // The refusal left the review unfinished, holdable and completable.
        assert_eq!(
            db.tasks().get(review).unwrap().status,
            crate::model::Status::Claimed
        );
    }

    #[test]
    fn a_verdict_on_a_task_that_is_not_a_review_is_refused() {
        let db = db();
        let seq = db.tasks().create(PROJECT, "t", "", 0, "cli").unwrap().seq;
        db.tasks().claim(seq, "codex:9f2c", TTL).unwrap();
        let err = db
            .tasks()
            .complete_with(seq, "codex:9f2c", "done", Some(Verdict::Upheld))
            .unwrap_err()
            .to_string();
        assert!(err.contains("not a review"), "{err}");
    }

    #[test]
    fn upheld_marks_the_work_without_moving_it() {
        let db = db();
        let (work, review) = reviewed_work(&db, "codex:9f2c");
        db.tasks().claim(review, "claude-code:af31", TTL).unwrap();
        let finished = db
            .tasks()
            .complete_with(
                review,
                "claude-code:af31",
                "read it, it holds",
                Some(Verdict::Upheld),
            )
            .unwrap();

        assert_eq!(finished.verdicts.len(), 1);
        assert_eq!(finished.verdicts[0].verdict, Verdict::Upheld);
        assert!(!finished.verdicts[0].reopened);
        assert_eq!(
            db.tasks().get(work).unwrap().status,
            crate::model::Status::Done
        );

        let latest = db.verdicts().latest(work).unwrap().unwrap();
        assert_eq!(latest.review_seq, review);
        assert_eq!(latest.worker, "codex:9f2c");
        assert_eq!(latest.reviewer, "claude-code:af31");
        assert!(latest.describe().contains("upheld by claude-code"));

        let standing = db
            .verdicts()
            .standing(&ProjectScope::Only(PROJECT.into()))
            .unwrap();
        assert_eq!(standing.get(&work), Some(&Verdict::Upheld));
    }

    #[test]
    fn sent_back_reopens_the_work_carrying_the_findings() {
        let db = db();
        let (work, review) = reviewed_work(&db, "codex:9f2c");
        db.tasks().claim(review, "claude-code:af31", TTL).unwrap();
        let finished = db
            .tasks()
            .complete_with(
                review,
                "claude-code:af31",
                "the error path drops the lock; re-take it before returning",
                Some(Verdict::SentBack),
            )
            .unwrap();
        assert!(finished.verdicts[0].reopened);

        let reopened = db.tasks().get(work).unwrap();
        assert_eq!(reopened.status, crate::model::Status::Open);
        assert_eq!(reopened.result, None);
        // The original brief survives, and the findings arrive appended, named.
        assert!(
            reopened.body.contains("keep the precedence"),
            "{}",
            reopened.body
        );
        assert!(
            reopened
                .body
                .contains(&format!("Sent back by review {review} (claude-code)")),
            "{}",
            reopened.body
        );
        assert!(
            reopened.body.contains("drops the lock"),
            "{}",
            reopened.body
        );

        // The author may take their own work back up — the bar was on the
        // review, never on the fix.
        db.tasks().claim(work, "codex:9f2c", TTL).unwrap();
    }

    /// The reopen's fallout: an agent mid-task on top of the sent-back work
    /// gets the shift on its trail in the same transaction, and an agent whose
    /// task is merely open gets nothing — readiness re-derives for it anyway.
    #[test]
    fn sending_work_back_lands_on_the_trail_of_whoever_builds_on_it() {
        let db = db();
        let (work, review) = reviewed_work(&db, "codex:9f2c");
        let live = db
            .tasks()
            .create(PROJECT, "use the loader", "", 0, "cli")
            .unwrap()
            .seq;
        let idle = db
            .tasks()
            .create(PROJECT, "document the loader", "", 0, "cli")
            .unwrap()
            .seq;
        db.deps().add(live, work, "cli").unwrap();
        db.deps().add(idle, work, "cli").unwrap();
        db.tasks().claim(live, "copilot:11", TTL).unwrap();

        db.tasks().claim(review, "claude-code:af31", TTL).unwrap();
        db.tasks()
            .complete_with(
                review,
                "claude-code:af31",
                "the error path drops the lock",
                Some(Verdict::SentBack),
            )
            .unwrap();

        let events_of = |seq: i64| {
            let id = db.tasks().get(seq).unwrap().id;
            db.tasks().events(&id, 40).unwrap()
        };
        let shifted: Vec<_> = events_of(live)
            .into_iter()
            .filter(|e| e.kind == crate::model::EventKind::GroundShifted)
            .collect();
        assert_eq!(shifted.len(), 1);
        assert!(
            shifted[0].detail.contains(&format!("task {work}")),
            "{shifted:?}"
        );
        assert!(
            shifted[0].detail.contains(&format!("review {review}")),
            "{shifted:?}"
        );
        assert!(
            !events_of(idle)
                .iter()
                .any(|e| e.kind == crate::model::EventKind::GroundShifted),
            "an open dependent is re-gated by readiness; writing to its trail would be noise"
        );
    }

    /// The whole loop, unattended: sent back, redone, re-reviewed, upheld —
    /// with the human nowhere in the transport.
    #[test]
    fn the_loop_runs_to_an_upheld_verdict_without_a_human() {
        let db = db();
        let (work, review) = reviewed_work(&db, "codex:9f2c");
        db.tasks().claim(review, "claude-code:af31", TTL).unwrap();
        db.tasks()
            .complete_with(
                review,
                "claude-code:af31",
                "misses the empty case",
                Some(Verdict::SentBack),
            )
            .unwrap();

        // Round two: the author fixes their own work; finishing files a fresh
        // review, because the first one is finished business.
        db.tasks().claim(work, "codex:9f2c", TTL).unwrap();
        let redone = db
            .tasks()
            .complete(work, "codex:9f2c", "empty case handled")
            .unwrap();
        let second = redone.review.expect("a fresh review for the redo");
        assert_ne!(second, review);

        db.tasks().claim(second, "claude-code:af31", TTL).unwrap();
        db.tasks()
            .complete_with(
                second,
                "claude-code:af31",
                "holds now",
                Some(Verdict::Upheld),
            )
            .unwrap();

        assert_eq!(
            db.tasks().get(work).unwrap().status,
            crate::model::Status::Done
        );
        let rounds = db.verdicts().for_task(work).unwrap();
        assert_eq!(rounds.len(), 2, "one verdict per round");
        assert_eq!(rounds[0].verdict, Verdict::SentBack);
        assert_eq!(rounds[1].verdict, Verdict::Upheld);
        assert_eq!(
            db.verdicts()
                .standing(&ProjectScope::Only(PROJECT.into()))
                .unwrap()
                .get(&work),
            Some(&Verdict::Upheld)
        );
    }

    /// The queue does not overrule a human's move: work already reopened (or
    /// cancelled) when the verdict lands stays exactly where the human put it.
    #[test]
    fn sent_back_does_not_overrule_a_human_who_already_moved_the_work() {
        let db = db();
        let (work, review) = reviewed_work(&db, "codex:9f2c");
        db.tasks()
            .reopen(work, "cli", "redoing this myself")
            .unwrap();
        db.tasks().claim(review, "claude-code:af31", TTL).unwrap();
        let finished = db
            .tasks()
            .complete_with(
                review,
                "claude-code:af31",
                "broken anyway",
                Some(Verdict::SentBack),
            )
            .unwrap();

        assert!(!finished.verdicts[0].reopened);
        let work_task = db.tasks().get(work).unwrap();
        assert_eq!(work_task.status, crate::model::Status::Open);
        assert!(
            !work_task.body.contains("Sent back"),
            "no findings are appended when nothing was reopened: {}",
            work_task.body
        );
        // But the verdict is on the record, and the trail says what happened.
        assert_eq!(db.verdicts().for_task(work).unwrap().len(), 1);
    }

    #[test]
    fn the_record_tallies_both_sides_of_every_verdict() {
        let db = db();
        // codex ships one task that is sent back once and then upheld…
        let (work, review) = reviewed_work(&db, "codex:9f2c");
        db.tasks().claim(review, "claude-code:af31", TTL).unwrap();
        db.tasks()
            .complete_with(review, "claude-code:af31", "no", Some(Verdict::SentBack))
            .unwrap();
        db.tasks().claim(work, "codex:9f2c", TTL).unwrap();
        let second = db
            .tasks()
            .complete(work, "codex:9f2c", "fixed")
            .unwrap()
            .review
            .unwrap();
        db.tasks().claim(second, "claude-code:af31", TTL).unwrap();
        db.tasks()
            .complete_with(second, "claude-code:af31", "yes", Some(Verdict::Upheld))
            .unwrap();
        // …and claude-code ships one that is upheld first pass, by codex.
        let (_, other_review) = reviewed_work(&db, "claude-code:af31");
        db.tasks().claim(other_review, "codex:9f2c", TTL).unwrap();
        db.tasks()
            .complete_with(other_review, "codex:9f2c", "clean", Some(Verdict::Upheld))
            .unwrap();

        let record = db
            .verdicts()
            .record(&ProjectScope::Only(PROJECT.into()), Axis::Harness)
            .unwrap();
        let of = |who: &str| record.iter().find(|r| r.who == who).unwrap();

        let codex = of("codex");
        assert_eq!(codex.judged, 2);
        assert_eq!(codex.upheld, 1);
        assert_eq!(codex.sent_back, 1);
        assert_eq!(codex.tasks_judged, 1);
        assert_eq!(codex.first_pass, 0, "sent back on the first round");
        assert_eq!(codex.upheld_given, 1);
        assert_eq!(codex.sent_back_given, 0);

        let claude = of("claude-code");
        assert_eq!(claude.judged, 1);
        assert_eq!(claude.tasks_judged, 1);
        assert_eq!(claude.first_pass, 1);
        assert_eq!(claude.upheld_given, 1);
        assert_eq!(claude.sent_back_given, 1);
    }

    /// Two people on one harness are one row by harness and two by person —
    /// the whole reason the axis exists.
    #[test]
    fn the_record_can_be_read_by_person() {
        let db = Db::open_in_memory().unwrap();
        // Ana ships on Claude Code, Ben reads it on Codex; then they swap
        // harnesses and do it again. Recusal forces the swap — a review is
        // barred to the harness that did the work — which is exactly what
        // makes the two axes disagree.
        let (_, ana_review) = reviewed_work(&db, "ana/claude-code:af31");
        db.tasks().claim(ana_review, "ben/codex:9f2c", TTL).unwrap();
        db.tasks()
            .complete_with(ana_review, "ben/codex:9f2c", "good", Some(Verdict::Upheld))
            .unwrap();

        let (_, ben_review) = reviewed_work(&db, "ben/claude-code:1a2b");
        db.tasks().claim(ben_review, "ana/codex:3c4d", TTL).unwrap();
        db.tasks()
            .complete_with(
                ben_review,
                "ana/codex:3c4d",
                "nope",
                Some(Verdict::SentBack),
            )
            .unwrap();

        // By harness: one model shipped everything and the other read it all.
        let by_harness = db
            .verdicts()
            .record(&ProjectScope::Only(PROJECT.into()), Axis::Harness)
            .unwrap();
        let of = |rows: &[crate::model::RecordRow], who: &str| {
            rows.iter().find(|r| r.who == who).unwrap().clone()
        };
        assert_eq!(of(&by_harness, "claude-code").judged, 2);
        assert_eq!(of(&by_harness, "claude-code").upheld_given, 0);
        assert_eq!(of(&by_harness, "codex").judged, 0);
        assert_eq!(of(&by_harness, "codex").upheld_given, 1);
        assert_eq!(of(&by_harness, "codex").sent_back_given, 1);

        // By person: the same verdicts say both of them shipped one and read
        // one. Same rows, different question.
        let by_person = db
            .verdicts()
            .record(&ProjectScope::Only(PROJECT.into()), Axis::Person)
            .unwrap();
        assert_eq!(by_person.len(), 2);
        assert_eq!(of(&by_person, "ana").judged, 1);
        assert_eq!(of(&by_person, "ana").upheld, 1);
        assert_eq!(of(&by_person, "ana").first_pass, 1);
        assert_eq!(of(&by_person, "ana").sent_back_given, 1);
        assert_eq!(of(&by_person, "ben").judged, 1);
        assert_eq!(of(&by_person, "ben").sent_back, 1);
        assert_eq!(of(&by_person, "ben").upheld_given, 1);
    }

    /// An unattributed actor has no person to credit, and the record says
    /// nothing rather than guessing one.
    #[test]
    fn reading_by_person_leaves_out_what_names_nobody() {
        let db = Db::open_in_memory().unwrap();
        let (_, review) = reviewed_work(&db, "claude-code:af31");
        db.tasks().claim(review, "codex:9f2c", TTL).unwrap();
        db.tasks()
            .complete_with(review, "codex:9f2c", "ok", Some(Verdict::Upheld))
            .unwrap();

        assert_eq!(
            db.verdicts()
                .record(&ProjectScope::Only(PROJECT.into()), Axis::Harness)
                .unwrap()
                .len(),
            2
        );
        assert!(db
            .verdicts()
            .record(&ProjectScope::Only(PROJECT.into()), Axis::Person)
            .unwrap()
            .is_empty());
    }

    /// Failing a review is not delivering a verdict: the reviewer could not do
    /// the reading, and the work stays exactly as it was.
    #[test]
    fn failing_a_review_delivers_nothing() {
        let db = db();
        let (work, review) = reviewed_work(&db, "codex:9f2c");
        db.tasks().claim(review, "claude-code:af31", TTL).unwrap();
        db.tasks()
            .fail(review, "claude-code:af31", "cannot build this branch")
            .unwrap();
        assert!(db.verdicts().for_task(work).unwrap().is_empty());
        assert_eq!(
            db.tasks().get(work).unwrap().status,
            crate::model::Status::Done
        );
    }
}