magi-cli 0.260.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
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
//! Owner-requested Git-only passes over a retained winner.
use anyhow::{Context as _, Result, bail, ensure};
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

use crate::{
    git,
    queue::{CompletionActor, CompletionContext, OperatorResume, Task, TaskStatus},
    run::{BaseSync, RunState, RunStatus},
};

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Request {
    pub run: String,
    pub at: Timestamp,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Pass {
    pub request: Request,
    pub(crate) pass_index: usize,
    pub required_base: String,
    pub tip: Option<String>,
    pub head_before: Option<String>,
    pub head_after: Option<String>,
    pub result: Outcome,
    pub verification_allowed: bool,
    #[serde(default)]
    reservation: Option<usize>,
    // Only this exact commit may have been installed by an interrupted pass.
    prepared_head: Option<String>,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum Outcome {
    Incomplete,
    AlreadyCurrent,
    Synchronized,
    AlreadyInBase,
    Stopped(String),
}

impl Outcome {
    pub(crate) fn succeeded(&self) -> bool {
        matches!(
            self,
            Self::AlreadyCurrent | Self::Synchronized | Self::AlreadyInBase
        )
    }
}

impl Pass {
    pub(crate) fn new(request: Request, state: &RunState) -> Self {
        Self {
            request,
            pass_index: state.passes.len(),
            required_base: format!("{}/{}", state.config.merge.remote, state.base_branch),
            tip: None,
            head_before: None,
            head_after: None,
            result: Outcome::Incomplete,
            reservation: None,
            verification_allowed: false,
            prepared_head: None,
        }
    }

    pub fn summary(&self) -> String {
        match &self.result {
            Outcome::Incomplete => {
                "Base synchronization only: incomplete; verification was not allowed. Resume with --sync-base-only to finish".into()
            }
            Outcome::AlreadyCurrent => {
                "HEAD already contains the base; verification was not allowed. Explicitly resume to continue"
                    .into()
            }
            Outcome::Synchronized => {
                "Base synchronized; verification was not allowed. Explicitly resume to continue"
                    .into()
            }
            Outcome::AlreadyInBase => {
                "Winner already integrated into the base; verification was not allowed".into()
            }
            Outcome::Stopped(why) => {
                format!("Base synchronization only: {why}; verification was not allowed")
            }
        }
    }

    pub(crate) fn report_line(&self) -> String {
        format!(
            "{} — {} @ {}; HEAD {} → {}; {}",
            self.request.at,
            self.required_base,
            self.tip.as_deref().unwrap_or("not fetched"),
            self.head_before.as_deref().unwrap_or("unknown"),
            self.head_after.as_deref().unwrap_or("unknown"),
            self.summary(),
        )
    }
}

pub(crate) fn validate_run(state: &RunState, home: &Path) -> Result<()> {
    ensure!(
        matches!(
            state.status,
            RunStatus::Reviewing
                | RunStatus::Gating
                | RunStatus::Blocked
                | RunStatus::Failed
                | RunStatus::Voting
        ) && state.tally.as_ref().is_some_and(|t| t.met_quorum)
            && state
                .winner()
                .is_some_and(|winner| winner.viable() && !winner.folded)
            && state.prep_failure.is_none(),
        "sync-base-only requires a stopped run with a decided, retained winner"
    );
    ensure!(
        !state.base_sync_blocked(),
        "sync-base-only cannot replace an existing base synchronization stop. {}",
        state.base_sync_next_action()
    );
    ensure!(
        state.pr.is_none()
            && state.pr_pushed_head.is_none()
            && state.pr_recovery.is_none()
            && !state.pr_publication_pending
            && state.merge.is_none()
            && state.land_approval.is_none()
            && state.merge_decision.is_none()
            && state.merge_repairs.is_empty()
            && state.land_rebase_from.is_none()
            && !state.base_recovery_pending()
            && state.github_text.is_none()
            && state
                .repair_grants
                .last()
                .is_none_or(|grant| grant.phase == crate::repair_budget::Phase::Complete),
        "sync-base-only cannot change a publication, landing or repair checkpoint"
    );
    ensure!(
        state
            .base_sync_recovery
            .operations
            .last()
            .is_none_or(|op| op.finished)
            && !home
                .join("runs")
                .join(&state.id)
                .join("base-sync")
                .try_exists()?,
        "an unfinished ordinary base sync requires its existing recovery path"
    );
    Ok(())
}

/// Queue under the owning task's claim, including explicit recovery of a dead worker.
pub fn queue(task: &mut Task, home: &Path, run: &str) -> Result<()> {
    let state = crate::resume::validate(home, run)?;
    crate::pr_head::ensure_owner_record(&state, None)?;
    validate_run(&state, home)?;
    let orphan = task
        .sync_only
        .as_ref()
        .filter(|request| task.status == TaskStatus::Running && request.run == run);
    let _ownership = if orphan.is_some() {
        let owner = crate::pr_head::owner_lock(home, run)?;
        let disk = RunState::load_under(run, home)?;
        ensure!(
            state.sync_only_passes == disk.sync_only_passes,
            "sync-only state changed during admission"
        );
        let claimed = crate::daemon::is_working_on_other(home, run, Timestamp::now())?;
        ensure!(
            disk.liveness_under(home, claimed) == crate::run::Liveness::Dead,
            "the recorded sync-only driver is live or its exit is unproven"
        );
        Some((owner, crate::driver::maintenance(home, run)?))
    } else {
        None
    };
    ensure!(
        task.execution_runs().last() == Some(&state.id)
            && (orphan.is_some()
                || matches!(
                    task.status,
                    TaskStatus::Held | TaskStatus::Parked | TaskStatus::Failed
                ))
            && !task.fresh_start
            && task.pr_recovery.is_none()
            && !crate::repair_budget::pending(task, &state),
        "sync-base-only requires the task's latest held execution with no pending replacement or repair"
    );
    let request = state
        .pending_sync_only()
        .map(|pass| pass.request.clone())
        .or_else(|| {
            orphan
                .filter(|request| {
                    !state
                        .sync_only_passes
                        .iter()
                        .any(|pass| &pass.request == *request)
                })
                .cloned()
        })
        .unwrap_or_else(|| Request {
            run: state.id.clone(),
            at: Timestamp::now(),
        });
    let at = Timestamp::now();
    crate::resume::release_standing(task);
    task.operator_decided_at = Some(at);
    task.resume_override = Some(OperatorResume {
        question_id: String::new(),
        at: request.at,
        conductor_rehold: None,
        forced: true,
        pinned_run: Some(state.id.clone()),
    });
    task.sync_only = Some(request);
    Ok(())
}

pub(crate) fn settle(
    task: &mut Task,
    request: &Request,
    state: Option<&RunState>,
    detail: Option<String>,
) {
    let pass = state
        .and_then(|state| state.sync_only_passes.last())
        .filter(|pass| pass.request == *request);
    let reason = detail
        .or_else(|| pass.map(Pass::summary))
        .unwrap_or_else(|| {
            "Base synchronization only: interrupted before start; verification was not allowed"
                .into()
        });
    task.sync_only = None;
    task.resume_override = None;
    if let Some(state) = state.filter(|state| state.status == RunStatus::AlreadyInBase)
        && pass.is_some_and(|pass| pass.result == Outcome::AlreadyInBase)
    {
        task.succeed(CompletionContext::from_run(
            CompletionActor::DaemonSettle,
            Some(reason.clone()),
            task,
            state,
        ));
        task.last_error = Some(reason);
        return;
    }
    task.last_error = Some(reason.clone());
    task.hold_after_sync_only(reason);
}

struct Checkout {
    path: PathBuf,
    disabled_filters: Vec<String>,
}

impl Checkout {
    async fn raw(&self, args: &[&str]) -> Result<git::GitOut> {
        let mut command = Vec::with_capacity(11 + 2 * self.disabled_filters.len() + args.len());
        command.extend([
            "--no-optional-locks",
            "-c",
            "core.hooksPath=/dev/null",
            "-c",
            "core.fsmonitor=false",
            "-c",
            "submodule.recurse=false",
            "-c",
            "maintenance.auto=false",
            "-c",
            "gc.auto=0",
        ]);
        for config in &self.disabled_filters {
            command.extend(["-c", config.as_str()]);
        }
        command.extend_from_slice(args);
        git::git_raw_command(&self.path, &command, true).await
    }

    async fn git(&self, args: &[&str]) -> Result<String> {
        let out = self.raw(args).await?;
        ensure!(out.ok(), "Git {} failed: {}", args[0], out.stderr);
        Ok(out.stdout)
    }
}

async fn checkout(state: &RunState) -> Result<Checkout> {
    let winner = state.winner().context("retained winner is missing")?;
    let mut git = Checkout {
        path: winner
            .worktree
            .canonicalize()
            .context("retained winner checkout is missing")?,
        disabled_filters: Vec::new(),
    };
    let top = PathBuf::from(git.git(&["rev-parse", "--show-toplevel"]).await?).canonicalize()?;
    let holder = git::worktree_holding(&state.repo, &winner.branch).await?;
    ensure!(
        top == git.path
            && state.repo.canonicalize()? != git.path
            && git.path.join(".git").is_file()
            && holder.and_then(|p| p.canonicalize().ok()).as_ref() == Some(&git.path)
            && git.git(&["symbolic-ref", "HEAD"]).await? == format!("refs/heads/{}", winner.branch),
        "winner checkout no longer holds its recorded branch"
    );
    let drivers = git
        .raw(&["config", "--get-regexp", r"^merge\..*\.driver$"])
        .await?;
    ensure!(
        drivers.code == Some(1),
        "sync-base-only refuses external merge drivers or unreadable Git configuration"
    );
    let filters = git
        .raw(&[
            "config",
            "--name-only",
            "--get-regexp",
            r"^filter\..*\.(clean|smudge|process)$",
        ])
        .await?;
    ensure!(
        matches!(filters.code, Some(0 | 1)),
        "cannot inspect configured Git filters"
    );
    let names: std::collections::BTreeSet<_> = filters
        .stdout
        .lines()
        .filter_map(|key| key.rsplit_once('.').map(|(name, _)| name))
        .collect();
    // An unused filter (e.g. Git for Windows' global LFS entry) is harmless.
    // A matching attribute must fail, not call a program or write raw content.
    for name in names {
        for setting in ["clean=", "smudge=", "process=", "required=true"] {
            git.disabled_filters.push(format!("{name}.{setting}"));
        }
    }
    ensure!(
        git.git(&[
            "status",
            "--porcelain",
            "--untracked-files=all",
            "--ignore-submodules=dirty"
        ])
        .await?
        .is_empty(),
        "winner checkout is dirty"
    );
    let git_dir = PathBuf::from(git.git(&["rev-parse", "--absolute-git-dir"]).await?);
    for marker in [
        "MERGE_HEAD",
        "CHERRY_PICK_HEAD",
        "REVERT_HEAD",
        "rebase-apply",
        "rebase-merge",
        "sequencer",
    ] {
        ensure!(
            !git_dir.join(marker).try_exists()?,
            "winner checkout has an unfinished Git operation"
        );
    }
    Ok(git)
}

async fn fetch_base(checkout: &Checkout, state: &RunState) -> Result<String> {
    let remote = &state.config.merge.remote;
    let branch = &state.base_branch;
    let refspec = format!("+refs/heads/{branch}:refs/remotes/{remote}/{branch}");
    let args = [
        "fetch",
        "--quiet",
        "--no-auto-maintenance",
        "--no-tags",
        "--no-prune",
        "--no-prune-tags",
        "--recurse-submodules=no",
        "--",
        remote,
        &refspec,
    ];
    let budget = std::time::Duration::from_secs(state.config.graph.timeout_resource_wait);
    tokio::time::timeout(budget, checkout.git(&args))
        .await
        .context("fetching the required base timed out")??;
    // FETCH_HEAD belongs to the linked checkout, unlike the tracking ref which
    // the scheduler can move independently of this fetch.
    checkout
        .git(&["rev-parse", "--verify", "FETCH_HEAD^{commit}"])
        .await
}

async fn public_integrated(
    checkout: &Checkout,
    state: &RunState,
    head: &str,
    tip: &str,
) -> Result<bool> {
    let branch = state.pr_branch().context("public branch is missing")?;
    let remote = &state.config.merge.remote;
    let reference = format!("refs/heads/{branch}");
    let budget = std::time::Duration::from_secs(state.config.graph.timeout_resource_wait);
    let observed = tokio::time::timeout(
        budget,
        checkout.raw(&[
            "ls-remote",
            "--exit-code",
            "--refs",
            "--",
            remote,
            &reference,
        ]),
    )
    .await
    .context("checking the public branch timed out")??;
    if observed.code == Some(2) {
        return Ok(true);
    }
    ensure!(
        observed.ok(),
        "checking the public branch failed: {}",
        observed.stderr
    );
    let (sha, name) = observed
        .stdout
        .split_once('\t')
        .context("unreadable public branch")?;
    ensure!(name == reference, "unexpected public branch response");
    if sha == head {
        return Ok(true);
    }
    tokio::time::timeout(
        budget,
        checkout.git(&[
            "fetch",
            "--quiet",
            "--no-auto-maintenance",
            "--no-tags",
            "--no-prune",
            "--no-prune-tags",
            "--recurse-submodules=no",
            "--refmap=",
            "--",
            remote,
            &reference,
        ]),
    )
    .await
    .context("fetching the public branch timed out")??;
    let theirs = checkout
        .git(&["rev-parse", "--verify", "FETCH_HEAD^{commit}"])
        .await?;
    ensure!(theirs == sha, "public branch moved during synchronization");
    let tree = checkout
        .raw(&["merge-tree", "--write-tree", &theirs, tip])
        .await?;
    if tree.code == Some(1) {
        return Ok(false);
    }
    ensure!(
        tree.ok(),
        "public branch merge-tree failed: {}",
        tree.stderr
    );
    Ok(tree.stdout
        == checkout
            .git(&["rev-parse", &format!("{tip}^{{tree}}")])
            .await?)
}

pub(crate) async fn sync(state: &mut RunState) -> Result<Outcome> {
    let checkout = checkout(state).await?;
    let _outputs = crate::retention::OutputLease::take(&checkout.path, true)?;
    let head = checkout.git(&["rev-parse", "HEAD"]).await?;
    let pass = state.sync_only_passes.last_mut().unwrap();
    pass.head_after = Some(head.clone());
    let saved = pass.reservation.is_some() || pass.prepared_head.is_some();
    let tip = if saved {
        let op = state
            .base_sync_recovery
            .operations
            .last()
            .context("interrupted synchronization has no budget reservation")?;
        ensure!(
            pass.reservation.is_none_or(|id| id == op.id)
                && pass.head_before.as_ref() == Some(&op.head)
                && pass.tip.as_ref() == Some(&op.tip)
                && (op.head == head || pass.prepared_head.as_ref() == Some(&head)),
            "interrupted synchronization has unrecognized reservation or HEAD; inspect the retained checkout"
        );
        op.tip.clone()
    } else {
        pass.head_before = Some(head.clone());
        pass.tip = None;
        state.save()?;
        let tip = fetch_base(&checkout, state).await?;
        state.sync_only_passes.last_mut().unwrap().tip = Some(tip.clone());
        state.save()?;
        tip
    };
    let behind = git::commits_ahead(&checkout.path, &head, &tip).await?;
    let attempts = state.base_sync.as_ref().map_or(0, |sync| sync.attempts);
    let tree = if behind != 0
        && state
            .sync_only_passes
            .last()
            .unwrap()
            .prepared_head
            .is_none()
    {
        let tree = checkout
            .raw(&["merge-tree", "--write-tree", &head, &tip])
            .await?;
        if tree.ok()
            && head != state.base_commit
            && tree.stdout
                == checkout
                    .git(&["rev-parse", &format!("{tip}^{{tree}}")])
                    .await?
        {
            return already_integrated(&checkout, state, &head, tip, behind).await;
        }
        Some(tree)
    } else {
        None
    };
    if behind != 0 && !saved {
        state.ensure_base_episode();
        if state.base_episode_used() >= crate::graph::BASE_SYNC_ROUNDS {
            let why = "base synchronization budget is exhausted";
            stop(
                state,
                tip,
                behind,
                attempts,
                &head,
                why,
                crate::base_sync_recovery::StopKind::Exhausted,
            );
            bail!("{why}");
        }
        state.base_sync = Some(BaseSync {
            tip: tip.clone(),
            behind,
            attempts,
            conflict: None,
            already_in: None,
        });
        state.reserve_base_operation(&head, &tip);
        // There is no scratch rebase. The pass itself owns replay of this
        // spent reservation; record both together before building the merge.
        state.sync_only_passes.last_mut().unwrap().reservation =
            state.base_sync_recovery.operations.last().map(|op| op.id);
        state.finish_base_operation(None);
        state.save()?;
    }
    if behind != 0 {
        let prepared =
            if let Some(commit) = state.sync_only_passes.last().unwrap().prepared_head.clone() {
                commit
            } else {
                let tree = tree.expect("merge tree computed for an unprepared synchronization");
                if tree.code == Some(1) {
                    let why = format!(
                        "base merge conflicted; winner left unchanged ({})",
                        tree.stdout
                            .lines()
                            .skip(1)
                            .take(5)
                            .collect::<Vec<_>>()
                            .join("; ")
                    );
                    stop(
                        state,
                        tip,
                        behind,
                        state.base_sync.as_ref().unwrap().attempts,
                        &head,
                        &why,
                        crate::base_sync_recovery::StopKind::Conflict,
                    );
                    bail!("{why}");
                }
                ensure!(tree.ok(), "Git merge-tree failed: {}", tree.stderr);
                let prepared = checkout
                    .git(&[
                        "-c",
                        "user.name=magi candidate",
                        "-c",
                        "user.email=magi@localhost",
                        "-c",
                        "commit.gpgSign=false",
                        "commit-tree",
                        &tree.stdout,
                        "-p",
                        &head,
                        "-p",
                        &tip,
                        "-m",
                        "Merge base before verification",
                    ])
                    .await?;
                state.sync_only_passes.last_mut().unwrap().prepared_head = Some(prepared.clone());
                state.save()?;
                prepared
            };
        ensure!(
            checkout.git(&["rev-parse", "HEAD"]).await? == head,
            "winner HEAD moved during base synchronization"
        );
        let updated = checkout
            .raw(&[
                "merge",
                "--ff-only",
                "--no-autostash",
                "--no-edit",
                "--no-verify-signatures",
                "--no-overwrite-ignore",
                &prepared,
            ])
            .await;
        state.sync_only_passes.last_mut().unwrap().head_after =
            checkout.git(&["rev-parse", "HEAD"]).await.ok();
        let updated = updated?;
        ensure!(
            updated.ok()
                && state.sync_only_passes.last().unwrap().head_after.as_ref() == Some(&prepared),
            "could not install the synchronized HEAD: {}",
            updated.stderr
        );
    }
    let head = checkout.git(&["rev-parse", "HEAD"]).await?;
    state.sync_only_passes.last_mut().unwrap().head_after = Some(head.clone());
    ensure!(
        git::is_ancestor(&checkout.path, &tip, &head).await,
        "winner does not contain the fetched base"
    );
    if head != state.base_commit
        && (head == tip
            || saved
                && checkout
                    .git(&["rev-parse", &format!("{tip}^{{tree}}")])
                    .await?
                    == checkout
                        .git(&["rev-parse", &format!("{head}^{{tree}}")])
                        .await?)
    {
        return already_integrated(&checkout, state, &head, tip, behind).await;
    }
    state.sync_only_passes.last_mut().unwrap().prepared_head = None;
    if saved || behind != 0 {
        state.finish_base_operation(Some(&head));
    } else {
        state.reconcile_verification_head(&head);
    }
    state.base_sync = Some(BaseSync {
        tip,
        behind: 0,
        attempts: state.base_sync.as_ref().map_or(0, |sync| sync.attempts),
        conflict: None,
        already_in: None,
    });
    state.base_sync_recovery.stop = None;
    Ok(if !saved && behind == 0 {
        Outcome::AlreadyCurrent
    } else {
        Outcome::Synchronized
    })
}

async fn already_integrated(
    checkout: &Checkout,
    state: &mut RunState,
    head: &str,
    tip: String,
    behind: usize,
) -> Result<Outcome> {
    if !public_integrated(checkout, state, head, &tip).await? {
        let why = "public branch contains changes not integrated into the fetched base";
        stop(
            state,
            tip,
            behind,
            state.base_sync.as_ref().map_or(0, |sync| sync.attempts),
            head,
            why,
            crate::base_sync_recovery::StopKind::Divergence,
        );
        bail!("{why}");
    }
    state.sync_only_passes.last_mut().unwrap().prepared_head = None;
    state.status = RunStatus::AlreadyInBase;
    state.base_sync_recovery.stop = None;
    state.base_sync = Some(BaseSync {
        already_in: Some(crate::already::Evidence {
            proof: crate::already::Proof::Tree,
            tip: tip.clone(),
            commits: Vec::new(),
        }),
        tip,
        behind,
        attempts: state.base_sync.as_ref().map_or(0, |sync| sync.attempts),
        conflict: None,
    });
    Ok(Outcome::AlreadyInBase)
}

fn stop(
    state: &mut RunState,
    tip: String,
    behind: usize,
    attempts: usize,
    head: &str,
    why: &str,
    kind: crate::base_sync_recovery::StopKind,
) {
    state.base_sync = Some(BaseSync {
        tip,
        behind,
        attempts,
        conflict: Some(why.into()),
        already_in: None,
    });
    state.event("land", why);
    state.record_base_sync_stop_as(head, Some(kind));
}