sloop-daemon 0.4.0

Agentic coding scheduler — a daemon that runs background coding agents autonomously in isolated git worktrees
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
//! Integration coverage for panel result checks, driven end to end by
//! scripted fake reviewers.
//!
//! The aggregation itself is unit-tested in `src/flow.rs` without any process
//! at all; what these tests prove is the part a pure function cannot — that
//! real reviewer processes get credentials scoped to their own seat, that
//! their reports land on the `(stage, attempt, reviewer)` row the credential
//! names, and that the verdict the walk acts on is the one those rows imply.

mod support;

use std::fs;
use std::path::{Path, PathBuf};

use serde_json::Value;
use support::{World, wait_until_slow};

const PANEL_PROMPT: &str = "Judge the work on this branch.\n";

fn shell_quote(value: &str) -> String {
    format!("'{}'", value.replace('\'', "'\"'\"'"))
}

fn sloop_binary() -> String {
    shell_quote(env!("CARGO_BIN_EXE_sloop"))
}

fn write_script(world: &World, name: &str, body: &str) -> PathBuf {
    let path = world.root().join(name);
    fs::write(&path, format!("#!/bin/sh\nset -eu\n{body}")).expect("write script");
    path
}

/// A reviewer that reports once and exits cleanly, saving the daemon's reply
/// where the test can read it. The exit is deliberately 0 whatever it
/// concluded — a reviewer that failed the work is not a reviewer that failed.
fn reviewer(world: &World, name: &str, verdict: &str, reason: &str, confidence: &str) -> PathBuf {
    write_script(
        world,
        &format!("{name}.sh"),
        &format!(
            "{sloop} --json verdict {verdict} --reason {reason} --confidence {confidence} \
             > {name}.out 2> {name}.err || true\nexit 0\n",
            sloop = sloop_binary(),
            reason = shell_quote(reason),
        ),
    )
}

/// A reviewer that exits without reporting anything at all.
fn silent_reviewer(world: &World, name: &str) -> PathBuf {
    write_script(world, &format!("{name}.sh"), "exit 0\n")
}

/// The build agent every panel scenario opens with: one commit, so the run has
/// something real to merge.
fn builder(world: &World) -> PathBuf {
    write_script(
        world,
        "builder.sh",
        "git -c user.name=agent -c user.email=agent@example.invalid commit --quiet \
         --allow-empty -m build\nexit 0\n",
    )
}

/// Writes the config, the flow, and the panel prompt file. `targets` is the
/// `(name, script)` pairs the flow's reviewers name.
fn configure(world: &World, flow: &str, targets: &[(&str, &Path)]) {
    let sloop_dir = world.root().join(".agents/sloop");
    fs::create_dir_all(sloop_dir.join("flows")).expect("create flow directory");
    fs::create_dir_all(sloop_dir.join("prompts")).expect("create prompt directory");
    fs::write(sloop_dir.join("prompts/panel.md"), PANEL_PROMPT).expect("write panel prompt");
    fs::write(sloop_dir.join("flows/default.yaml"), flow).expect("write flow");

    let mut config = String::from(
        "version: 1\nscheduler:\n  max_parallel_tasks: 1\nagent:\n  default_target: builder\n  targets:\n",
    );
    for (name, script) in targets {
        config.push_str(&format!(
            "    {name}:\n      cmd: [\"sh\", {}, \"{{prompt}}\"]\n",
            serde_json::to_string(&script.to_string_lossy()).expect("serialize script path"),
        ));
    }
    fs::write(sloop_dir.join("config.yaml"), config).expect("write config");
}

fn post(world: &World, name: &str) -> String {
    let ticket = world.write_ticket(name, "# Panel scenario\n");
    let output = world.sloop(&["post", ticket.to_str().expect("UTF-8 path"), "--manual"]);
    assert!(
        output.status.success(),
        "post failed: {}",
        String::from_utf8_lossy(&output.stderr)
    );
    World::json_stdout(&output)["data"]["ticket"]["id"]
        .as_str()
        .expect("ticket id")
        .to_owned()
}

fn status(world: &World) -> Value {
    let output = world.sloop(&["status"]);
    assert!(output.status.success());
    World::json_stdout(&output)["data"].clone()
}

fn settled(world: &World) -> bool {
    let tickets = &status(world)["tickets"];
    tickets["merged"].as_i64().unwrap_or(0)
        + tickets["needs_review"].as_i64().unwrap_or(0)
        + tickets["failed"].as_i64().unwrap_or(0)
        > 0
}

/// Every `panel_report` row for a run, in evidence order, as the raw JSON the
/// daemon persisted. Reading the rows rather than the rendered aggregate is
/// deliberate: the aggregate is derived, so the rows are the only thing there
/// is to be right about.
fn panel_reports(world: &World, run_id: &str) -> Vec<Value> {
    let connection = rusqlite::Connection::open(world.db_path()).expect("open state database");
    let mut statement = connection
        .prepare(
            "SELECT data_json FROM run_evidence
             WHERE run_id = ?1 AND kind = 'panel_report' ORDER BY sequence",
        )
        .expect("prepare panel report query");
    statement
        .query_map([run_id], |row| row.get::<_, String>(0))
        .expect("query panel reports")
        .map(|data| serde_json::from_str(&data.expect("read report JSON")).expect("report is JSON"))
        .collect()
}

/// The `show` row for one named stage of a run.
fn shown_stage(world: &World, run: &str, stage: &str) -> Value {
    world.show_snapshot(run)["stages"]
        .as_array()
        .expect("stages array")
        .iter()
        .find(|row| row["stage"] == stage)
        .unwrap_or_else(|| panic!("no `{stage}` stage in the run's show output"))
        .clone()
}

fn worktree_json(world: &World, name: &str) -> Value {
    let path = world.run_worktree(1).join(name);
    let text = fs::read_to_string(&path)
        .unwrap_or_else(|error| panic!("read {}: {error}", path.display()));
    serde_json::from_str(&text)
        .unwrap_or_else(|error| panic!("{name} is not JSON: {error}\n{text}"))
}

/// A three-seat panel over a trivially passing action, with the stage's fail
/// action spelled out by the caller.
fn three_seat_flow(quorum: u32, fail_action: &str) -> String {
    format!(
        "- {{ name: build, action: agent, result_check: {{ exec: ['true'] }} }}\n\
         - name: review\n  \
           action: {{ exec: ['true'] }}\n  \
           result_check:\n    \
             panel:\n      \
               prompt: prompts/panel.md\n      \
               reviewers: [{{ target: alpha }}, {{ target: beta }}, {{ target: gamma }}]\n      \
               require: {{ quorum: {quorum} }}\n  \
           fail_action: {fail_action}\n\
         - {{ name: merge, action: {{ builtin: merge }}, result_check: none }}\n"
    )
}

/// The quorum is met, so the stage passes and the walk carries on to the
/// merge. The dissenting reviewer is still recorded: a panel that agreed and
/// one that was outvoted must not read the same afterwards.
#[test]
fn a_two_of_three_quorum_passes_and_the_run_proceeds() {
    let world = World::configured();
    let build = builder(&world);
    let alpha = reviewer(&world, "alpha", "pass", "reads correct", "high");
    let beta = reviewer(&world, "beta", "pass", "tests cover it", "medium");
    let gamma = reviewer(&world, "gamma", "fail", "naming could be better", "low");
    configure(
        &world,
        &three_seat_flow(2, "fail"),
        &[
            ("builder", &build),
            ("alpha", &alpha),
            ("beta", &beta),
            ("gamma", &gamma),
        ],
    );
    world.commit_all("initial");
    world.start_daemon();
    let ticket = post(&world, "panel-quorum-met.md");
    assert!(world.sloop(&["run", &ticket]).status.success());

    wait_until_slow("the panel-approved run merges", || {
        status(&world)["tickets"]["merged"] == 1
    });

    let run = world.run_id(1);
    let reports = panel_reports(&world, &run);
    assert_eq!(reports.len(), 3, "{reports:?}");
    for (seat, expected) in [(0, "pass"), (1, "pass"), (2, "fail")] {
        let report = reports
            .iter()
            .find(|report| report["reviewer"] == seat)
            .unwrap_or_else(|| panic!("no report for seat {seat}: {reports:?}"));
        assert_eq!(report["verdict"], expected, "{report}");
        assert_eq!(report["stage"], "review");
        assert_eq!(report["attempt"], 1);
    }
    assert_eq!(reports[2]["confidence"], "low");
    assert_eq!(reports[2]["reason"], "naming could be better");

    // The stage's verdict names the panel as its source and carries the tally,
    // not any one reviewer's words.
    let review = shown_stage(&world, &run, "review");
    assert_eq!(review["state"], "passed");
    assert_eq!(review["verdict_source"], "panel");
    assert_eq!(review["reason"], "panel: 2 of 3 reviewers passed, quorum 2");

    // Every seat is listed, dissent included.
    let seats = review["reviewers"].as_array().expect("reviewer rows");
    assert_eq!(seats.len(), 3);
    assert_eq!(seats[0]["target"], "alpha");
    assert_eq!(seats[2]["verdict"], "fail");
    assert_eq!(seats[2]["confidence"], "low");
    assert_eq!(seats[2]["reason"], "naming could be better");

    // And all of it reaches the text an operator actually reads. A tally in
    // the envelope that the renderer drops is a tally nobody sees.
    let text = String::from_utf8_lossy(&world.sloop_plain(&["show", &world.run_alias(1)]).stdout)
        .into_owned();
    for line in [
        "review  passed",
        "verdict from panel",
        "alpha  pass  confidence high  reads correct",
        "beta   pass  confidence medium  tests cover it",
        "gamma  fail  confidence low  naming could be better",
    ] {
        assert!(text.contains(line), "show missed {line:?}:\n{text}");
    }
}

/// One pass out of three is short of a two-vote quorum, so the stage fails —
/// and its `fail_action` decides what that costs. `continue` is advisory, so
/// the walk records the failure and carries on.
#[test]
fn a_one_of_three_panel_fails_the_stage_and_its_fail_action_applies() {
    let world = World::configured();
    let build = builder(&world);
    let alpha = reviewer(&world, "alpha", "pass", "acceptable", "medium");
    let beta = reviewer(&world, "beta", "fail", "missing a test", "high");
    let gamma = reviewer(&world, "gamma", "fail", "leaks a handle", "high");
    configure(
        &world,
        &three_seat_flow(2, "continue"),
        &[
            ("builder", &build),
            ("alpha", &alpha),
            ("beta", &beta),
            ("gamma", &gamma),
        ],
    );
    world.commit_all("initial");
    world.start_daemon();
    let ticket = post(&world, "panel-quorum-missed.md");
    assert!(world.sloop(&["run", &ticket]).status.success());

    wait_until_slow("the advisory panel failure settles", || settled(&world));

    let run = world.run_id(1);
    let review = shown_stage(&world, &run, "review");
    assert_eq!(review["state"], "failed");
    assert_eq!(review["verdict_source"], "panel");
    assert_eq!(review["reason"], "panel: 1 of 3 reviewers passed, quorum 2");

    // `continue` is what makes this an advisory failure rather than a halt:
    // the merge after it still ran.
    assert_eq!(shown_stage(&world, &run, "merge")["state"], "passed");
    assert_eq!(status(&world)["tickets"]["merged"], 1);
}

/// A reviewer that exits without reporting has approved nothing. Its seat
/// counts as a `Fail`, and `show` says so in the same words the rest of the
/// system uses for an unreported verdict.
#[test]
fn a_silent_reviewer_counts_as_a_fail() {
    let world = World::configured();
    let build = builder(&world);
    let alpha = reviewer(&world, "alpha", "pass", "reads correct", "high");
    let beta = silent_reviewer(&world, "beta");
    let gamma = reviewer(&world, "gamma", "pass", "tests cover it", "medium");
    configure(
        &world,
        &three_seat_flow(3, "fail"),
        &[
            ("builder", &build),
            ("alpha", &alpha),
            ("beta", &beta),
            ("gamma", &gamma),
        ],
    );
    world.commit_all("initial");
    world.start_daemon();
    let ticket = post(&world, "panel-silent-reviewer.md");
    assert!(world.sloop(&["run", &ticket]).status.success());

    wait_until_slow("the silent reviewer fails the stage", || settled(&world));

    let run = world.run_id(1);
    // Only the reviewers that spoke left rows; the silent seat is filled in by
    // the aggregation, never written.
    let reports = panel_reports(&world, &run);
    assert_eq!(reports.len(), 2, "{reports:?}");
    assert!(
        reports.iter().all(|report| report["reviewer"] != 1),
        "{reports:?}"
    );

    let review = shown_stage(&world, &run, "review");
    assert_eq!(review["state"], "failed");
    assert_eq!(review["reason"], "panel: 2 of 3 reviewers passed, quorum 3");
    let seats = review["reviewers"].as_array().expect("reviewer rows");
    assert_eq!(seats[1]["target"], "beta");
    assert_eq!(seats[1]["verdict"], "fail");
    assert_eq!(seats[1]["reason"], "no verdict reported");
    // Nothing was heard from it, so nothing is claimed about how sure it was.
    assert_eq!(seats[1]["confidence"], Value::Null);
    assert_eq!(status(&world)["tickets"]["merged"], 0);
}

/// The credential authorises exactly one report. A reviewer that tries to
/// revise its vote is refused, and the first report is what the panel counts.
#[test]
fn a_reviewers_second_verdict_call_is_denied() {
    let world = World::configured();
    let build = builder(&world);
    let alpha = write_script(
        &world,
        "alpha.sh",
        &format!(
            "{sloop} --json verdict fail --reason 'first word' > alpha.out 2> alpha.err\n\
             {sloop} --json verdict pass --reason 'changed my mind' \
             > second.out 2> second.err || true\n\
             exit 0\n",
            sloop = sloop_binary(),
        ),
    );
    let beta = reviewer(&world, "beta", "pass", "acceptable", "medium");
    let gamma = reviewer(&world, "gamma", "pass", "acceptable", "medium");
    configure(
        &world,
        &three_seat_flow(3, "fail"),
        &[
            ("builder", &build),
            ("alpha", &alpha),
            ("beta", &beta),
            ("gamma", &gamma),
        ],
    );
    world.commit_all("initial");
    world.start_daemon();
    let ticket = post(&world, "panel-one-shot.md");
    assert!(world.sloop(&["run", &ticket]).status.success());

    wait_until_slow("the one-shot reviewer settles the run", || settled(&world));

    let first = worktree_json(&world, "alpha.out");
    assert_eq!(first["ok"], true, "{first}");
    assert_eq!(first["data"]["verdict"]["reviewer"], 0);
    assert_eq!(first["data"]["verdict"]["verdict"], "fail");
    // Absent from the call, so recorded as the documented default.
    assert_eq!(first["data"]["verdict"]["confidence"], "medium");

    let second = worktree_json(&world, "second.err");
    assert_eq!(second["ok"], false, "{second}");
    assert_eq!(second["error"]["code"], "conflict");

    // The refused call changed nothing: one row, holding the first word.
    let reports = panel_reports(&world, &world.run_id(1));
    let alpha_reports: Vec<&Value> = reports
        .iter()
        .filter(|report| report["reviewer"] == 0)
        .collect();
    assert_eq!(alpha_reports.len(), 1, "{reports:?}");
    assert_eq!(alpha_reports[0]["verdict"], "fail");
    assert_eq!(alpha_reports[0]["reason"], "first word");
}

/// A reviewer's token names its own seat and nothing else. Holding a
/// colleague's token buys nothing — not that seat, not any other.
#[test]
fn a_reviewers_token_reports_only_for_its_own_seat() {
    let world = World::configured();
    let build = builder(&world);
    let stash = world.root().join("alpha.token");
    // The first seat leaks its credential where the second can pick it up.
    let alpha = write_script(
        &world,
        "alpha.sh",
        &format!(
            "printf '%s\\n%s\\n' \"$SLOOP_SOCKET\" \"$SLOOP_TOKEN\" > {stash}\n\
             {sloop} --json verdict pass --reason 'acceptable' > alpha.out 2>&1 || true\n\
             exit 0\n",
            stash = shell_quote(&stash.to_string_lossy()),
            sloop = sloop_binary(),
        ),
    );
    // The second seat presents the first's token before its own.
    let beta = write_script(
        &world,
        "beta.sh",
        &format!(
            "SLOOP_SOCKET=$(sed -n 1p {stash})\n\
             SLOOP_TOKEN=$(sed -n 2p {stash})\n\
             export SLOOP_SOCKET SLOOP_TOKEN\n\
             {sloop} --json verdict pass --reason 'stolen credential' \
             > stolen.out 2> stolen.err || true\n\
             exit 0\n",
            stash = shell_quote(&stash.to_string_lossy()),
            sloop = sloop_binary(),
        ),
    );
    let gamma = reviewer(&world, "gamma", "pass", "acceptable", "medium");
    configure(
        &world,
        &three_seat_flow(3, "fail"),
        &[
            ("builder", &build),
            ("alpha", &alpha),
            ("beta", &beta),
            ("gamma", &gamma),
        ],
    );
    world.commit_all("initial");
    world.start_daemon();
    let ticket = post(&world, "panel-seat-scoped-token.md");
    assert!(world.sloop(&["run", &ticket]).status.success());

    wait_until_slow("the run with a stolen credential settles", || {
        settled(&world)
    });

    // The stolen token is not the credential the live socket was issued
    // against, so the daemon refuses it outright.
    let stolen = worktree_json(&world, "stolen.err");
    assert_eq!(stolen["ok"], false, "{stolen}");
    assert_eq!(stolen["error"]["code"], "unauthorized");

    // And it reported for nobody: seat 0 holds only alpha's own report, and
    // seat 1 — whose reviewer spent its turn on the theft — holds none.
    let reports = panel_reports(&world, &world.run_id(1));
    let seats: Vec<&Value> = reports.iter().map(|report| &report["reviewer"]).collect();
    assert_eq!(
        reports
            .iter()
            .filter(|report| report["reviewer"] == 0)
            .count(),
        1,
        "{seats:?}"
    );
    assert!(
        reports.iter().all(|report| report["reviewer"] != 1),
        "{seats:?}"
    );
    // A quorum of three cannot be met with a seat unreported, so the theft
    // cost the run exactly what silence would have.
    let review = shown_stage(&world, &world.run_id(1), "review");
    assert_eq!(review["state"], "failed");
    assert_eq!(review["reason"], "panel: 2 of 3 reviewers passed, quorum 3");
}

/// A reviewer's token dies with its turn. Once the seat is done, the same
/// secret buys nothing in the stage that follows it or in any later run — the
/// socket is bound to a run and validates exactly one live credential at a
/// time.
#[test]
fn a_reviewers_token_cannot_report_for_a_later_stage_or_another_run() {
    let world = World::configured();
    let build = builder(&world);
    let stash = world.root().join("alpha.token");
    // Seat 0 stashes its credential the first time it runs, and thereafter
    // always tries the stashed one before its own. In the first run that
    // stashed token *is* its own and the report lands; in the second it is a
    // credential from a run that has already settled.
    let alpha = write_script(
        &world,
        "alpha.sh",
        &format!(
            "if [ ! -f {stash} ]; then printf '%s' \"$SLOOP_TOKEN\" > {stash}; fi\n\
             SLOOP_TOKEN=$(cat {stash}) {sloop} --json verdict pass --reason 'stashed credential' \
             > stashed.out 2> stashed.err || true\n\
             {sloop} --json verdict pass --reason 'own credential' > own.out 2>&1 || true\n\
             exit 0\n",
            stash = shell_quote(&stash.to_string_lossy()),
            sloop = sloop_binary(),
        ),
    );
    let beta = reviewer(&world, "beta", "pass", "acceptable", "medium");
    // A `reported` stage after the panel, presenting the reviewer's token
    // before its own. One script proves both halves of the stage boundary: the
    // superseded credential is refused, and the stage's real one still works.
    let sign_off = write_script(
        &world,
        "sign-off.sh",
        &format!(
            "MINE=$SLOOP_TOKEN\n\
             SLOOP_TOKEN=$(cat {stash}) {sloop} --json verdict fail --reason 'stale' \
             > stale.out 2> stale.err || true\n\
             SLOOP_TOKEN=$MINE {sloop} --json verdict pass --reason 'signed off' \
             > signed.out 2>&1 || true\n\
             exit 0\n",
            stash = shell_quote(&stash.to_string_lossy()),
            sloop = sloop_binary(),
        ),
    );
    configure(
        &world,
        &format!(
            "- {{ name: build, action: agent, result_check: {{ exec: ['true'] }} }}\n\
             - name: review\n  \
               action: {{ exec: ['true'] }}\n  \
               result_check:\n    \
                 panel:\n      \
                   prompt: prompts/panel.md\n      \
                   reviewers: [{{ target: alpha }}, {{ target: beta }}]\n      \
                   require: {{ quorum: 2 }}\n\
             - name: sign_off\n  \
               action: {{ exec: [\"sh\", {script}] }}\n  \
               result_check: reported\n",
            script = serde_json::to_string(&sign_off.to_string_lossy()).expect("quote path"),
        ),
        &[("builder", &build), ("alpha", &alpha), ("beta", &beta)],
    );
    world.commit_all("initial");
    world.start_daemon();
    let first = post(&world, "panel-stale-token.md");
    assert!(world.sloop(&["run", &first]).status.success());

    // `> signed.out` creates the file when the redirection opens, before
    // `sloop` has written a byte into it, so waiting on existence alone races
    // the write and can read an empty file. Parseable JSON is the first state
    // that actually means the command finished. It is also the last write the
    // script makes, so `stale.err` is complete by the time this passes.
    wait_until_slow("the first run finishes its sign-off", || {
        fs::read_to_string(world.run_worktree(1).join("signed.out"))
            .ok()
            .is_some_and(|text| serde_json::from_str::<Value>(&text).is_ok())
    });

    // A later stage of the very same run cannot use the reviewer's token: the
    // credential the socket validates against was replaced when this stage
    // minted its own.
    let stale = worktree_json(&world, "stale.err");
    assert_eq!(stale["ok"], false, "{stale}");
    assert_eq!(stale["error"]["code"], "unauthorized");
    // Its own token still works, so the refusal is about the credential and
    // not about the socket having been shut.
    let signed = worktree_json(&world, "signed.out");
    assert_eq!(signed["ok"], true, "{signed}");
    assert_eq!(signed["data"]["verdict"]["stage"], "sign_off");

    // A second ticket through the same flow. Seat 0 now replays the settled
    // run's credential against a socket that never issued it.
    let second = post(&world, "panel-foreign-token.md");
    assert!(world.sloop(&["run", &second]).status.success());
    wait_until_slow("the second run finishes its panel", || {
        panel_reports(&world, &world.run_id(2)).len() == 2
    });

    let foreign: Value = serde_json::from_str(
        &fs::read_to_string(world.run_worktree(2).join("stashed.err")).expect("read stashed.err"),
    )
    .expect("stashed.err is JSON");
    assert_eq!(foreign["ok"], false, "{foreign}");
    assert_eq!(foreign["error"]["code"], "unauthorized");

    // The foreign token reported for nothing. Each run's seat 0 holds exactly
    // the one report its own live credential authorised.
    for (position, reason) in [(1, "stashed credential"), (2, "own credential")] {
        let reports = panel_reports(&world, &world.run_id(position));
        let seat_zero: Vec<&Value> = reports
            .iter()
            .filter(|report| report["reviewer"] == 0)
            .collect();
        assert_eq!(seat_zero.len(), 1, "run {position}: {reports:?}");
        assert_eq!(seat_zero[0]["reason"], reason, "run {position}");
    }
}

/// A seat's brief is its own credential read back: the stage it was minted
/// for, and the obligation that stage puts on a reviewer. Sloop's own panel
/// prompt tells a reviewer to change nothing, so a brief that told it to commit
/// would have the daemon contradicting itself in two strings it authored.
///
/// It is also the whole of what a seat learns: two seats of the same panel read
/// the identical brief, so nothing in it identifies a colleague.
#[test]
fn a_panel_seats_brief_names_its_stage_and_hides_the_other_seats() {
    let world = World::configured();
    let build = builder(&world);
    let briefing = |name: &str| {
        write_script(
            &world,
            &format!("{name}.sh"),
            &format!(
                "{sloop} --json brief > {name}-brief.json 2>&1 || true\n\
                 {sloop} --json verdict pass --reason 'acceptable' >/dev/null 2>&1 || true\n\
                 exit 0\n",
                sloop = sloop_binary(),
            ),
        )
    };
    let alpha = briefing("alpha");
    let beta = briefing("beta");
    configure(
        &world,
        "- { name: build, action: agent, result_check: { exec: ['true'] } }\n\
         - name: review\n  \
           action: { exec: ['true'] }\n  \
           result_check:\n    \
             panel:\n      \
               prompt: prompts/panel.md\n      \
               reviewers: [{ target: alpha }, { target: beta }]\n      \
               require: { quorum: 2 }\n\
         - { name: merge, action: { builtin: merge }, result_check: none }\n",
        &[("builder", &build), ("alpha", &alpha), ("beta", &beta)],
    );
    world.commit_all("initial");
    world.start_daemon();
    let ticket = post(&world, "panel-brief.md");
    assert!(world.sloop(&["run", &ticket]).status.success());

    wait_until_slow("the briefed panel merges", || {
        status(&world)["tickets"]["merged"] == 1
    });

    let alpha_brief = worktree_json(&world, "alpha-brief.json");
    assert_eq!(alpha_brief["ok"], true, "{alpha_brief}");
    // The stage comes from the credential, not from whatever the driver is
    // running: a seat's brief names the stage its token was minted for.
    assert_eq!(alpha_brief["data"]["stage"]["name"], "review");
    assert_eq!(alpha_brief["data"]["stage"]["attempt"], 1);
    assert_eq!(alpha_brief["data"]["stage"]["result_check"], "panel");
    let obligation = alpha_brief["data"]["definition_of_done"][0]
        .as_str()
        .expect("obligation text")
        .to_lowercase();
    assert!(
        obligation.contains("reported verdict"),
        "reviewer obligation: {obligation}"
    );
    assert!(
        !obligation.contains("commit"),
        "a panel reviewer was told to commit: {obligation}"
    );

    // Seat 0 and seat 1 read the same words. There is no seat index, no
    // colleague's target, and no other seat's report in a brief — so holding
    // one seat's credential reveals nothing about the panel around it. Only
    // the envelope's echoed request id differs, which is per-call and says
    // nothing about who asked.
    assert_eq!(
        alpha_brief["data"],
        worktree_json(&world, "beta-brief.json")["data"]
    );
}

/// A backward edge re-runs the panel, and the second round's reports belong to
/// the second round. Keying them by attempt is what keeps a converged loop
/// from being decided by the votes that sent it back.
#[test]
fn reports_land_on_the_right_attempt_after_a_return_to_re_run() {
    let world = World::configured();
    let build = builder(&world);
    // Both reviewers reject the first round and accept the second, counting
    // through a file outside the worktree so the tally survives the re-run.
    let turncoat = |name: &str| {
        let counter = world.root().join(format!("{name}.count"));
        fs::write(&counter, b"").expect("create counter");
        write_script(
            &world,
            &format!("{name}.sh"),
            &format!(
                "printf x >> {counter}\nrounds=$(wc -c < {counter} | tr -d ' ')\n\
                 if [ \"$rounds\" -le 1 ]; then\n  \
                   {sloop} --json verdict fail --reason 'round one' >/dev/null 2>&1 || true\n\
                 else\n  \
                   {sloop} --json verdict pass --reason 'round two' >/dev/null 2>&1 || true\n\
                 fi\nexit 0\n",
                counter = shell_quote(&counter.to_string_lossy()),
                sloop = sloop_binary(),
            ),
        )
    };
    let alpha = turncoat("alpha");
    let beta = turncoat("beta");
    configure(
        &world,
        "- { name: build, action: agent, result_check: { exec: ['true'] } }\n\
         - name: review\n  \
           action: { exec: ['true'] }\n  \
           result_check:\n    \
             panel:\n      \
               prompt: prompts/panel.md\n      \
               reviewers: [{ target: alpha }, { target: beta }]\n      \
               require: { quorum: 2 }\n  \
           fail_action: { return_to: build, attempts: 1 }\n\
         - { name: merge, action: { builtin: merge }, result_check: none }\n",
        &[("builder", &build), ("alpha", &alpha), ("beta", &beta)],
    );
    world.commit_all("initial");
    world.start_daemon();
    let ticket = post(&world, "panel-return-to.md");
    assert!(world.sloop(&["run", &ticket]).status.success());

    wait_until_slow("the re-reviewed run merges", || {
        status(&world)["tickets"]["merged"] == 1
    });

    // Four rows: two seats twice over, each round on its own attempt. The
    // first round's rejections are still there — a re-run supersedes a verdict
    // in the walk, it does not erase the evidence behind it.
    let run = world.run_id(1);
    let reports = panel_reports(&world, &run);
    let mut keyed: Vec<(u64, u64, String)> = reports
        .iter()
        .map(|report| {
            (
                report["attempt"].as_u64().expect("attempt"),
                report["reviewer"].as_u64().expect("reviewer"),
                report["verdict"].as_str().expect("verdict").to_owned(),
            )
        })
        .collect();
    keyed.sort();
    assert_eq!(
        keyed,
        vec![
            (1, 0, "fail".to_owned()),
            (1, 1, "fail".to_owned()),
            (2, 0, "pass".to_owned()),
            (2, 1, "pass".to_owned()),
        ]
    );

    // And the walk read them per attempt: the first execution of `review`
    // failed, the second passed.
    let executions: Vec<(u64, String, String)> = world.show_snapshot(&run)["stages"]
        .as_array()
        .expect("stages array")
        .iter()
        .filter(|row| row["stage"] == "review")
        .map(|row| {
            (
                row["attempt"].as_u64().unwrap_or(0),
                row["state"].as_str().unwrap_or("?").to_owned(),
                row["reason"].as_str().unwrap_or("").to_owned(),
            )
        })
        .collect();
    assert_eq!(
        executions,
        vec![
            (
                1,
                "failed".to_owned(),
                "panel: 0 of 2 reviewers passed, quorum 2".to_owned()
            ),
            (
                2,
                "passed".to_owned(),
                "panel: 2 of 2 reviewers passed, quorum 2".to_owned()
            ),
        ]
    );
}