magi-cli 0.45.0

Blind multi-agent implementation competition: N agents implement, M judges rank blind, deliberate, vote privately, winner survives double review + E2E gate
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
//! End-to-end: `magi review <branch>` on work that already exists.
//!
//! The point of the test is that no competition happens — no implementation, no
//! judging, no vote — and yet the review + verification + gate loop runs to a
//! decision. That property is not enforced by a flag anywhere; it falls out of
//! a run having a single viable candidate and a tally that is already decided,
//! so it is exactly the kind of thing that would rot silently.
mod common;

use common::{
    Judges, fixture, fixture_with_quota, fixture_with_review_seat_that_recovers_on_retry,
    fixture_with_silent_review_seat,
};
use magi::graph::Runner;
use magi::run::RunStatus;

fn run_git(repo: &std::path::Path, args: &[&str]) {
    let out = std::process::Command::new("git")
        .args(args)
        .current_dir(repo)
        .output()
        .expect("spawn git");
    assert!(
        out.status.success(),
        "git {args:?}: {}",
        String::from_utf8_lossy(&out.stderr)
    );
}

common::e2e! {
async fn a_review_only_run_reviews_an_existing_branch_without_competing() {
    // `fixture()` sets a process-wide home directory, so any two tests in
    // this binary that build one must not run concurrently — every test here
    // must take this lock, not just the slow one, or the two that don't will
    // still race each other underneath it.
    let _home = common::home_lock().await;
    let fx = fixture(_home, Judges::Unanimous, true);

    // Hand-written work on a branch: the thing `magi run` would never produce.
    run_git(&fx.repo, &["checkout", "-q", "-b", "feat/by-hand"]);
    std::fs::write(fx.repo.join("note.txt"), "written by a human\n").unwrap();
    run_git(&fx.repo, &["add", "-A"]);
    run_git(&fx.repo, &["commit", "-q", "-m", "add note.txt by hand"]);
    run_git(&fx.repo, &["checkout", "-q", "main"]);

    let mut runner = Runner::review(&fx.repo, "feat/by-hand", fx.config.clone())
        .await
        .expect("open a review-only run");

    // Before anything runs: one candidate, credited to nobody, already the
    // winner.
    assert_eq!(runner.state.candidates.len(), 1);
    let c = &runner.state.candidates[0];
    assert_eq!(c.label, 'A');
    assert_eq!(c.branch, "feat/by-hand");
    assert_eq!(c.commits, 1);
    assert!(
        !fx.config.agents.iter().any(|a| a.id == c.agent),
        "the candidate must not be attributed to a roster agent, got {}",
        c.agent
    );
    let tally = runner.state.tally.as_ref().expect("a decided tally");
    assert_eq!(tally.winner, 'A');
    assert_eq!(tally.rankings, 0, "nothing was ranked");

    runner.execute().await.expect("execute");
    let state = &runner.state;

    // No competition took place.
    assert!(state.judgements.is_empty(), "nobody judged");
    assert!(state.deliberation.is_empty(), "nobody deliberated");
    assert!(state.votes.is_empty(), "nobody voted");

    // The cheap half did: the fixture's reviewers raise one blocker on round 1,
    // the fixer commits, round 2 is clean.
    assert_eq!(state.reviews.len(), 2, "{:?}", state.reviews);
    let first = &state.reviews[0];
    assert_eq!(first.reviews.len(), 2);
    assert!(!first.clean);
    let fix = first.fix.as_ref().expect("the fixer ran");
    assert!(fix.committed, "fixes must land on the branch");
    assert!(state.reviews[1].clean);

    assert!(state.gate.iter().all(|o| o.ok()));
    assert_eq!(state.status, RunStatus::Ready);

    // The fix landed on the branch under review, not on a detached head.
    let winner = state.winner().expect("winner");
    assert!(winner.worktree.join("fixed.txt").is_file());
    let head = std::process::Command::new("git")
        .args(["rev-parse", "--abbrev-ref", "HEAD"])
        .current_dir(&winner.worktree)
        .output()
        .expect("spawn git");
    assert_eq!(
        String::from_utf8_lossy(&head.stdout).trim(),
        "feat/by-hand",
        "the review worktree must stay attached to the branch"
    );
}
}

common::e2e! {
async fn a_reviewer_that_never_answered_is_never_reported_as_a_clean_round() {
    let _home = common::home_lock().await;
    let mut fx = fixture_with_silent_review_seat(_home, &["review-2"]);
    // One round is enough to exercise "budget exhausted while incomplete".
    fx.config.graph.review_rounds = 1;

    run_git(&fx.repo, &["checkout", "-q", "-b", "feat/by-hand"]);
    std::fs::write(fx.repo.join("note.txt"), "written by a human\n").unwrap();
    run_git(&fx.repo, &["add", "-A"]);
    run_git(&fx.repo, &["commit", "-q", "-m", "add note.txt by hand"]);
    run_git(&fx.repo, &["checkout", "-q", "main"]);

    let mut runner = Runner::review(&fx.repo, "feat/by-hand", fx.config.clone())
        .await
        .expect("open a review-only run");
    runner.execute().await.expect("execute");
    let state = &runner.state;

    assert_eq!(state.reviews.len(), 1, "{:?}", state.reviews);
    for round in &state.reviews {
        // review-1 answered and found nothing; review-2 never came back. That
        // must never look the same as a panel that read the patch and passed
        // it: half the panel is not evidence of anything.
        assert_eq!(round.answered, 1);
        assert_eq!(round.expected, 2);
        assert!(round.incomplete());
        assert_eq!(round.blocking, 0, "the seat that did answer found nothing");
        assert!(
            !round.clean,
            "a round missing half its panel must never be reported clean: {round:?}"
        );
        let missing = round
            .reviews
            .iter()
            .find(|r| r.reviewer == 2)
            .expect("review-2's record is still in the round");
        assert!(
            missing.failed.is_some(),
            "a seat that never produced a usable answer is `failed`, whatever \
             `attempts` says: {missing:?}"
        );
    }
    // Nothing was ever raised to fix, so the fixer never ran.
    assert!(state.reviews.iter().all(|r| r.fix.is_none()));
    assert_eq!(state.status, RunStatus::Blocked);
}
}

common::e2e! {
/// The addendum's third gap: a seat that times out and then answers on
/// `ask_json_wave`'s nudge must read as recovered, not as silent — the two
/// looked identical in history (a retry event and nothing else) before
/// `ReviewRecord::attempts` existed.
async fn a_reviewer_that_only_answers_on_retry_is_recorded_as_recovered_not_silent() {
    let _home = common::home_lock().await;
    let mut fx = fixture_with_review_seat_that_recovers_on_retry(_home, &["review-2"]);
    fx.config.graph.review_rounds = 1;

    run_git(&fx.repo, &["checkout", "-q", "-b", "feat/by-hand"]);
    std::fs::write(fx.repo.join("note.txt"), "written by a human\n").unwrap();
    run_git(&fx.repo, &["add", "-A"]);
    run_git(&fx.repo, &["commit", "-q", "-m", "add note.txt by hand"]);
    run_git(&fx.repo, &["checkout", "-q", "main"]);

    let mut runner = Runner::review(&fx.repo, "feat/by-hand", fx.config.clone())
        .await
        .expect("open a review-only run");
    runner.execute().await.expect("execute");
    let state = &runner.state;

    assert_eq!(state.reviews.len(), 1, "{:?}", state.reviews);
    let round = &state.reviews[0];
    assert_eq!(round.answered, 2, "both seats answered, one on a retry");
    assert!(!round.incomplete());
    let recovered = round
        .reviews
        .iter()
        .find(|r| r.reviewer == 2)
        .expect("review-2's record is still in the round");
    assert!(
        recovered.failed.is_none(),
        "a seat that did eventually answer is not `failed`: {recovered:?}"
    );
    assert!(
        recovered.attempts > 0,
        "recorded attempts must show the retry actually happened: {recovered:?}"
    );
    assert!(
        state
            .events
            .iter()
            .any(|e| e.node == "review" && e.message.contains("retry 1")),
        "the retry itself is also visible in the event log: {:?}",
        state.events
    );
}
}

common::e2e! {
async fn a_reviewer_rate_limited_by_quota_gates_clean_on_the_answered_panel() {
    // The field report this closes: a rate-limited reviewer must not be
    // waited on round after round hoping its session limit lifts — the
    // limit will not lift by the next round any more than it lifted between
    // this round's attempt and the retry `ask_json_wave` already refuses to
    // send. The round decides on whoever did answer instead.
    let _home = common::home_lock().await;
    let mut fx = fixture_with_quota(_home, &["review-2"]);
    // A generous budget: if the fix regresses to waiting for full quorum,
    // this would still resolve eventually and the `reviews.len() == 1`
    // assertion below is what would catch it, not a timeout.
    fx.config.graph.review_rounds = 5;

    run_git(&fx.repo, &["checkout", "-q", "-b", "feat/by-hand"]);
    std::fs::write(fx.repo.join("note.txt"), "written by a human\n").unwrap();
    run_git(&fx.repo, &["add", "-A"]);
    run_git(&fx.repo, &["commit", "-q", "-m", "add note.txt by hand"]);
    run_git(&fx.repo, &["checkout", "-q", "main"]);

    let mut runner = Runner::review(&fx.repo, "feat/by-hand", fx.config.clone())
        .await
        .expect("open a review-only run");
    runner.execute().await.expect("execute");
    let state = &runner.state;

    // Decided in the very first round, not after exhausting the budget
    // hoping review-2 comes back.
    assert_eq!(state.reviews.len(), 1, "{:?}", state.reviews);
    let round = &state.reviews[0];
    assert_eq!(round.answered, 1);
    assert_eq!(round.expected, 2);
    assert!(round.incomplete(), "review-2 never answered");
    assert!(
        round.clean,
        "a quota-lost seat must not block a decision the rest of the panel already made: \
         {round:?}"
    );

    // The exclusion is still on the record, not swallowed: `magi show`
    // reads both of these to report who was missing and why.
    let missing = round
        .reviews
        .iter()
        .find(|r| r.reviewer == 2)
        .expect("review-2's own record");
    assert!(
        missing
            .failed
            .as_deref()
            .is_some_and(|why| why.contains("rate limited")),
        "{missing:?}"
    );
    assert_eq!(state.quota.len(), 1, "{:?}", state.quota);
    assert_eq!(state.quota[0].seat, "review-2");

    assert_eq!(state.status, RunStatus::Ready);
}
}

common::e2e! {
async fn a_panel_lost_entirely_to_quota_still_waits_instead_of_deciding_on_nobody() {
    // The other half of the same fix: excluding a rate-limited seat from
    // quorum must never go so far as calling a round clean with nobody left
    // to have reviewed it. With every seat quota'd this falls back to the
    // pre-existing behaviour — wait out the round budget, then `Blocked` —
    // exactly as an ordinary (non-quota) silent panel already does.
    let _home = common::home_lock().await;
    let mut fx = fixture_with_quota(_home, &["review-1", "review-2"]);
    fx.config.graph.review_rounds = 1;

    run_git(&fx.repo, &["checkout", "-q", "-b", "feat/by-hand"]);
    std::fs::write(fx.repo.join("note.txt"), "written by a human\n").unwrap();
    run_git(&fx.repo, &["add", "-A"]);
    run_git(&fx.repo, &["commit", "-q", "-m", "add note.txt by hand"]);
    run_git(&fx.repo, &["checkout", "-q", "main"]);

    let mut runner = Runner::review(&fx.repo, "feat/by-hand", fx.config.clone())
        .await
        .expect("open a review-only run");
    runner.execute().await.expect("execute");
    let state = &runner.state;

    assert_eq!(state.reviews.len(), 1, "{:?}", state.reviews);
    let round = &state.reviews[0];
    assert_eq!(round.answered, 0);
    assert_eq!(round.expected, 2);
    assert!(
        !round.clean,
        "a panel with nobody left to review must never gate clean: {round:?}"
    );
    assert_eq!(state.quota.len(), 2, "{:?}", state.quota);
    assert_eq!(state.status, RunStatus::Blocked);
}
}

common::e2e! {
async fn review_refuses_the_cases_that_cannot_mean_anything() {
    let _home = common::home_lock().await;
    let fx = fixture(_home, Judges::Unanimous, false);

    let missing = Runner::review(&fx.repo, "no/such/branch", fx.config.clone()).await;
    assert!(missing.is_err(), "a branch that does not exist");

    // The base branch itself has nothing to review against.
    let base = Runner::review(&fx.repo, "main", fx.config.clone()).await;
    assert!(base.is_err(), "reviewing the base branch");

    // A branch with no commits beyond base is not a change.
    run_git(&fx.repo, &["branch", "feat/empty"]);
    let empty = Runner::review(&fx.repo, "feat/empty", fx.config.clone()).await;
    assert!(empty.is_err(), "a branch with no commits of its own");
}
}

fn rev(repo: &std::path::Path, rev: &str) -> String {
    let out = std::process::Command::new("git")
        .args(["rev-parse", rev])
        .current_dir(repo)
        .output()
        .expect("spawn git");
    assert!(out.status.success(), "rev-parse {rev}");
    String::from_utf8_lossy(&out.stdout).trim().to_owned()
}

/// A bare `origin` for `fx.repo` and a second clone that pushes to it, which
/// stands in for a jj workspace moving only the remote-tracking ref.
fn wire_origin(fx: &common::Fixture) -> std::path::PathBuf {
    let origin = fx.tmp.path().join("origin.git");
    run_git(
        fx.tmp.path(),
        &[
            "clone",
            "--bare",
            "-q",
            fx.repo.to_str().unwrap(),
            origin.to_str().unwrap(),
        ],
    );
    run_git(
        &fx.repo,
        &["remote", "add", "origin", origin.to_str().unwrap()],
    );
    let side = fx.tmp.path().join("side");
    run_git(
        fx.tmp.path(),
        &[
            "clone",
            "-q",
            origin.to_str().unwrap(),
            side.to_str().unwrap(),
        ],
    );
    run_git(&side, &["config", "user.name", "other"]);
    run_git(&side, &["config", "user.email", "other@example.com"]);
    side
}

/// Push `branch` from the sideline with a real commit, while `fx.repo` keeps a
/// local `branch` that is still the empty placeholder (or diverged).
fn push_real_work(side: &std::path::Path, branch: &str) {
    run_git(side, &["checkout", "-q", "-b", branch]);
    std::fs::write(side.join("real.txt"), "the real change\n").unwrap();
    run_git(side, &["add", "-A"]);
    run_git(side, &["commit", "-q", "-m", "the real change"]);
    run_git(side, &["push", "-q", "origin", branch]);
}

common::e2e! {
async fn a_stale_local_branch_is_fast_forwarded_to_the_pushed_work() {
    let _home = common::home_lock().await;
    let fx = fixture(_home, Judges::Unanimous, true);
    let side = wire_origin(&fx);
    run_git(&fx.repo, &["branch", "feat/pushed", "main"]);
    push_real_work(&side, "feat/pushed");
    let pushed = rev(&side, "HEAD");

    let runner = Runner::review(&fx.repo, "feat/pushed", fx.config.clone())
        .await
        .expect("open a review-only run");
    assert_eq!(rev(&fx.repo, "refs/heads/feat/pushed"), pushed);
    let c = &runner.state.candidates[0];
    assert!(c.worktree.join("real.txt").is_file());
    assert_eq!(c.commits, 1);
}
}

common::e2e! {
async fn a_diverged_branch_with_local_work_is_refused_and_leaves_no_worktree() {
    let _home = common::home_lock().await;
    let fx = fixture(_home, Judges::Unanimous, true);
    let side = wire_origin(&fx);
    run_git(&fx.repo, &["checkout", "-q", "-b", "feat/split"]);
    std::fs::write(fx.repo.join("local.txt"), "local only\n").unwrap();
    run_git(&fx.repo, &["add", "-A"]);
    run_git(&fx.repo, &["commit", "-q", "-m", "local only"]);
    run_git(&fx.repo, &["checkout", "-q", "main"]);
    let local = rev(&fx.repo, "feat/split");
    push_real_work(&side, "feat/split");

    let err = Runner::review(&fx.repo, "feat/split", fx.config.clone())
        .await
        .err()
        .expect("a diverged branch must not be reviewed");
    assert!(err.to_string().contains("diverged"), "{err}");
    assert_eq!(rev(&fx.repo, "refs/heads/feat/split"), local);
    let list = std::process::Command::new("git")
        .args(["worktree", "list"])
        .current_dir(&fx.repo)
        .output()
        .unwrap();
    assert!(!String::from_utf8_lossy(&list.stdout).contains("under-review"));
}
}

common::e2e! {
async fn a_rewritten_placeholder_gives_way_to_the_pushed_work() {
    let _home = common::home_lock().await;
    let fx = fixture(_home, Judges::Unanimous, true);
    let side = wire_origin(&fx);
    run_git(&fx.repo, &["checkout", "-q", "-b", "feat/rewritten"]);
    run_git(&fx.repo, &["commit", "-q", "--allow-empty", "-m", "placeholder"]);
    run_git(&fx.repo, &["checkout", "-q", "main"]);
    push_real_work(&side, "feat/rewritten");
    let pushed = rev(&side, "HEAD");

    let runner = Runner::review(&fx.repo, "feat/rewritten", fx.config.clone())
        .await
        .expect("open a review-only run");
    assert_eq!(rev(&fx.repo, "refs/heads/feat/rewritten"), pushed);
    assert!(runner.state.candidates[0].worktree.join("real.txt").is_file());
}
}

common::e2e! {
async fn a_branch_whose_tree_equals_the_base_is_refused_and_releases_its_worktree() {
    let _home = common::home_lock().await;
    let fx = fixture(_home, Judges::Unanimous, true);
    run_git(&fx.repo, &["checkout", "-q", "-b", "feat/placeholder"]);
    run_git(
        &fx.repo,
        &["commit", "-q", "--allow-empty", "-m", "placeholder"],
    );
    run_git(&fx.repo, &["checkout", "-q", "main"]);

    let err = Runner::review(&fx.repo, "feat/placeholder", fx.config.clone())
        .await
        .err()
        .expect("an empty-tree branch must not be reviewed");
    assert!(err.to_string().contains("tree identical"), "{err}");
    // The branch is not left locked by the aborted attempt.
    run_git(&fx.repo, &["worktree", "prune"]);
    let list = std::process::Command::new("git")
        .args(["worktree", "list"])
        .current_dir(&fx.repo)
        .output()
        .unwrap();
    assert!(!String::from_utf8_lossy(&list.stdout).contains("feat/placeholder"));
}
}