magi-cli 0.221.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
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
//! Display-only progress from structured records. Phase order is never evidence
//! that an earlier phase succeeded. This view cannot authorize an operation.
use crate::run::{E2eStatus, GateStatus, Liveness, RunState};
use serde::Serialize;

#[derive(Debug, Serialize)]
/// Shared presentation for list cards and run details.
pub struct Progress {
    /// Current activity, including waits and driver exit.
    pub activity: String,
    /// Ordered phases; ordering implies no success.
    pub phases: Vec<Phase>,
    /// Recorded rounds only; no guessed in-flight round number.
    pub rounds: Vec<ReviewProgress>,
}

#[derive(Debug, Serialize)]
/// A stage with its own evidence, independent of adjacent stages.
pub struct Phase {
    /// Stable DOM identity.
    pub key: &'static str,
    /// Human readable stage name.
    pub label: &'static str,
    /// Display state: current, paused, complete, failed, recorded, skipped, unrecorded, unknown or upcoming.
    pub state: &'static str,
    /// Recorded results or active seat names.
    pub detail: String,
}

/// A recorded review attempt, including verification and panel completeness.
#[derive(Debug, Serialize)]
pub struct ReviewProgress {
    /// The persisted round number.
    pub round: usize,
    /// Historical result; does not claim verification of a later head.
    pub detail: String,
}

fn phase(node: &str) -> &'static str {
    match node {
        "prep" => "prep",
        "advise" | "advise-synthesis" => "advice",
        "implement" | "implementing" => "implement",
        "judge" | "judging" | "deliberate" | "deliberating" | "vote" | "voting" => "selection",
        "review" | "reviewing" | "fix" | "verify" => "review",
        "gate" | "gating" | "gate-fix" => "gate",
        "land" | "landing" | "merge" => "land",
        _ => "other",
    }
}

fn implementation_results(s: &RunState) -> Option<String> {
    let n = s
        .candidates
        .iter()
        .filter(|c| c.duration_ms > 0 || c.failed.is_some() || c.verified_noop.is_some())
        .count();
    (n > 0).then(|| format!("{n} implementation results recorded"))
}

/// Compact activity shared by run cards and task navigation.
pub fn activity(s: &RunState, live: Liveness, waiting: bool) -> String {
    if waiting {
        "Waiting for your answer".to_owned()
    } else if let Some(w) = &s.resource_wait {
        format!("Waiting for {}", w.kind)
    } else if let Some(b) = &s.resource_block {
        format!("Held · {}", b.description())
    } else if s.parked {
        "Parked · work preserved".to_owned()
    } else if s.status.done() {
        s.status.as_str().replace('_', " ")
    } else if live == Liveness::Dead {
        "Driver exited · work preserved".to_owned()
    } else if live == Liveness::Unknown {
        format!("{} · driver unconfirmed", s.status.as_str())
    } else if !s.active.is_empty() {
        format!(
            "{} · {}",
            s.status.as_str(),
            s.active.keys().cloned().collect::<Vec<_>>().join(", ")
        )
    } else {
        format!("{} · no active seat recorded", s.status.as_str())
    }
}

/// Derive progress without mutating persisted state or interpreting event prose.
pub fn build(s: &RunState, live: Liveness, waiting: bool) -> Progress {
    let stopped =
        s.status.done() || s.parked || s.resource_block.is_some() || live == Liveness::Dead;
    let activity = activity(s, live, waiting);
    let review_only = crate::graph::is_review_run(s);
    let halted = matches!(
        s.status,
        crate::run::RunStatus::Blocked
            | crate::run::RunStatus::Failed
            | crate::run::RunStatus::Stalled
    );
    let current = if halted {
        s.resource_block
            .as_ref()
            .map(|b| phase(&b.node))
            .or_else(|| s.events.last().map(|e| phase(&e.node)))
            .unwrap_or("other")
    } else if s.status == crate::run::RunStatus::Prep
        && s.active.values().any(|a| phase(&a.node) == "advice")
    {
        "advice"
    } else {
        phase(s.status.as_str())
    };
    let mut phases = Vec::new();
    for (key, label, applicable, evidence) in [
        (
            "prep",
            "Prepare",
            true,
            s.prep_failure
                .as_ref()
                .map(|_| "Preparation failed".to_owned())
                .or_else(|| {
                    (s.prep_complete == Some(true)).then(|| "Preparation complete".to_owned())
                }),
        ),
        (
            "advice",
            "Design advice",
            !review_only && s.config.graph.advise,
            s.advice
                .as_ref()
                .map(|a| format!("{} advisor records", a.records.len())),
        ),
        (
            "implement",
            "Parallel implementation",
            !review_only,
            implementation_results(s),
        ),
        (
            "selection",
            "Selection",
            !review_only && s.config.graph.implementers > 1 && !s.judge_skipped,
            s.tally.as_ref().map(|_| "Decision recorded".to_owned()),
        ),
        (
            "review",
            "Review & verification",
            true,
            (!s.reviews.is_empty())
                .then(|| format!("{} recorded rounds · see results below", s.reviews.len())),
        ),
        (
            "gate",
            "Final gate",
            true,
            match s.gate_status() {
                GateStatus::NotRun => None,
                GateStatus::NotConfigured => Some("No commands configured".to_owned()),
                GateStatus::Passed => Some("Passed on recorded head".to_owned()),
                GateStatus::Failed => Some("Gate failed".to_owned()),
            },
        ),
        (
            "land",
            "Publish & land",
            true,
            if s.status == crate::run::RunStatus::Merged {
                Some("Merge confirmed".to_owned())
            } else {
                s.merge
                    .as_ref()
                    .map(|_| "Publication outcome recorded · see Landing".to_owned())
            },
        ),
    ] {
        let active: Vec<_> = s
            .active
            .iter()
            .filter(|(_, a)| phase(&a.node) == key)
            .map(|(k, _)| k.as_str())
            .collect();
        let here = (!s.status.done() || halted) && (!active.is_empty() || current == key);
        let state = if !applicable && !here && evidence.is_none() {
            "skipped"
        } else if here && (waiting || s.resource_wait.is_some() || stopped) {
            "paused"
        } else if here && live == Liveness::Unknown {
            "unconfirmed"
        } else if here {
            "current"
        } else if (key == "prep" && s.prep_failure.is_some())
            || (key == "gate" && s.gate_status() == GateStatus::Failed)
        {
            "failed"
        } else if (key == "prep" && s.prep_complete == Some(true))
            || (key == "selection" && s.tally.is_some())
            || (key == "gate" && s.gate_status() == GateStatus::Passed)
            || (key == "land" && s.status == crate::run::RunStatus::Merged)
        {
            "complete"
        } else if evidence.is_some() {
            "recorded"
        } else if s.status.done()
            || [
                "prep",
                "advice",
                "implement",
                "selection",
                "review",
                "gate",
                "land",
            ]
            .iter()
            .position(|p| *p == key)
            .zip(
                [
                    "prep",
                    "advice",
                    "implement",
                    "selection",
                    "review",
                    "gate",
                    "land",
                ]
                .iter()
                .position(|p| *p == current),
            )
            .is_some_and(|(step, now)| step < now)
        {
            "unrecorded"
        } else {
            "upcoming"
        };
        let detail = if !active.is_empty() {
            active.join(" · ")
        } else {
            evidence.unwrap_or_else(|| {
                match state {
                    "skipped" => "Not applicable to this run",
                    "unknown" | "unrecorded" => "No result recorded",
                    "current" => "Current phase",
                    "unconfirmed" => "Driver unconfirmed",
                    "paused" => "Stopped here",
                    _ => "No result yet",
                }
                .to_owned()
            })
        };
        phases.push(Phase {
            key,
            label,
            state,
            detail,
        });
    }
    if s.active.values().any(|a| phase(&a.node) == "other") {
        phases.push(Phase {
            key: "other",
            label: "Other activity",
            state: if stopped || waiting {
                "paused"
            } else if live == Liveness::Unknown {
                "unconfirmed"
            } else {
                "current"
            },
            detail: s
                .active
                .iter()
                .filter(|(_, a)| phase(&a.node) == "other")
                .map(|(k, a)| format!("{k} · {}", a.node))
                .collect::<Vec<_>>()
                .join(", "),
        });
    }
    let rounds = s
        .reviews
        .iter()
        .map(|r| {
            let verification = match r.e2e_status() {
                E2eStatus::Passed => "verification passed on recorded head",
                E2eStatus::Failed => "verification failed on recorded head",
                E2eStatus::Deferred => "verification deferred",
                E2eStatus::NotConfigured => "no verification commands configured",
                E2eStatus::ResourceBlocked => "verification resource blocked",
            };
            let panel = if r.expected > 0 {
                format!("{}/{} reviewers answered", r.answered, r.expected)
            } else {
                format!(
                    "{} reviewer records · panel completeness unknown",
                    r.reviews.len()
                )
            };
            ReviewProgress {
                round: r.round,
                detail: format!(
                    "{panel} · {verification}{}",
                    if r.fix.is_some() {
                        " · fix recorded"
                    } else {
                        ""
                    }
                ),
            }
        })
        .collect();
    Progress {
        activity,
        phases,
        rounds,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{config::Config, run::RunStatus};

    fn run() -> RunState {
        RunState::new(
            "repo".into(),
            "main".into(),
            "abc".into(),
            "task".into(),
            Config::default(),
        )
    }

    #[test]
    fn progress_does_not_infer_previous_success_from_phase_order() {
        let mut s = run();
        s.status = RunStatus::Gating;
        let p = build(&s, Liveness::Live, false);
        assert_eq!(
            p.phases.iter().find(|p| p.key == "gate").unwrap().state,
            "current"
        );
        assert!(!p.phases.iter().any(|p| p.state == "recorded"));
        assert_eq!(
            p.phases
                .iter()
                .find(|p| p.key == "implement")
                .unwrap()
                .state,
            "unrecorded"
        );
        assert_eq!(
            p.phases.iter().find(|p| p.key == "land").unwrap().state,
            "upcoming"
        );
        s.status = RunStatus::Merged;
        assert_eq!(
            build(&s, Liveness::Dead, false)
                .phases
                .iter()
                .find(|p| p.key == "implement")
                .unwrap()
                .state,
            "unrecorded"
        );
        assert!(
            !build(&s, Liveness::Dead, false)
                .phases
                .iter()
                .any(|p| matches!(p.state, "current" | "recorded"))
        );
    }

    #[test]
    fn progress_parallel_seats_waits_exit_and_unknown_remain_distinct() {
        let mut s = run();
        s.status = RunStatus::Implementing;
        for key in ["implement-A", "implement-B"] {
            s.seat_started("implement", key, std::time::Duration::from_secs(600), 0);
        }
        let p = build(&s, Liveness::Live, false);
        assert!(p.activity.contains("implement-A, implement-B"));
        assert!(
            p.phases
                .iter()
                .find(|p| p.key == "implement")
                .unwrap()
                .detail
                .contains("implement-B")
        );
        assert_eq!(
            build(&s, Liveness::Live, true).activity,
            "Waiting for your answer"
        );
        assert!(
            build(&s, Liveness::Unknown, false)
                .activity
                .contains("unconfirmed")
        );
        let dead = build(&s, Liveness::Dead, false);
        assert!(dead.activity.contains("Driver exited"));
        assert_eq!(
            dead.phases
                .iter()
                .find(|p| p.key == "implement")
                .unwrap()
                .state,
            "paused"
        );
        s.active.clear();
        s.seat_started(
            "new-node",
            "new-seat",
            std::time::Duration::from_secs(60),
            0,
        );
        assert_eq!(
            build(&s, Liveness::Live, false).phases.last().unwrap().key,
            "other"
        );
        assert_eq!(
            build(&s, Liveness::Live, false)
                .phases
                .iter()
                .find(|p| p.key == "implement")
                .unwrap()
                .state,
            "current"
        );
    }

    #[test]
    fn progress_resource_wait_and_stopped_panel_are_not_active_work() {
        let mut s = run();
        s.status = RunStatus::Reviewing;
        s.resource_wait = Some(crate::run::ResourceWait {
            node: "verify".into(),
            kind: "build cache".into(),
            holder: "another run".into(),
            since: jiff::Timestamp::now(),
            limit_secs: 60,
        });
        let p = build(&s, Liveness::Live, false);
        assert_eq!(p.activity, "Waiting for build cache");
        assert_eq!(
            p.phases.iter().find(|p| p.key == "review").unwrap().state,
            "paused"
        );
        s.resource_wait = None;
        s.status = RunStatus::Stalled;
        s.event("review", "Panel stopped");
        let p = build(&s, Liveness::Dead, false);
        assert_eq!(p.activity, "stalled");
        assert_eq!(
            p.phases.iter().find(|p| p.key == "review").unwrap().state,
            "paused"
        );
        s.status = RunStatus::Reviewing;
        assert_eq!(
            build(&s, Liveness::Unknown, false)
                .phases
                .iter()
                .find(|p| p.key == "review")
                .unwrap()
                .state,
            "unconfirmed"
        );
    }

    #[test]
    fn progress_review_only_skips_competition_and_preserves_gate_failure() {
        let mut s = run();
        s.reviewed_commits = Some(vec![]);
        s.status = RunStatus::Reviewing;
        let p = build(&s, Liveness::Live, false);
        assert_eq!(
            p.phases
                .iter()
                .find(|p| p.key == "implement")
                .unwrap()
                .state,
            "skipped"
        );
        assert_eq!(
            p.phases
                .iter()
                .find(|p| p.key == "selection")
                .unwrap()
                .state,
            "skipped"
        );
        assert!(
            p.phases
                .iter()
                .find(|p| p.key == "review")
                .unwrap()
                .detail
                .contains("Current")
        );
        s.status = RunStatus::Blocked;
        s.gate_ran = true;
        s.gate.push(crate::run::CommandOutcome {
            command: "check".into(),
            code: Some(1),
            output_tail: String::new(),
            duration_ms: 1,
            resource_blocked: false,
        });
        let p = build(&s, Liveness::Dead, false);
        assert_eq!(p.activity, "blocked");
        assert_eq!(
            p.phases.iter().find(|p| p.key == "gate").unwrap().detail,
            "Gate failed"
        );
    }
}