Skip to main content

magi/
handover.rs

1//! Taking a branch over from an earlier attempt at the same task.
2//!
3//! A retried task can reopen a branch (`magi/f82f/A`) that the *previous*
4//! run still has checked out in its own worktree. Git holds a branch in one
5//! worktree at a time, so the new run's review cannot start, and the only way
6//! forward used to be an agent asking the operator whether the old worktree
7//! may be deleted. Auto-fold cannot help: it only folds terminal runs, and an
8//! unfinished run keeps its worktrees on purpose so it can be resumed.
9//!
10//! When the run holding the branch is an earlier attempt at the same task
11//! ([`crate::queue::Task::earlier_attempts`]) - superseded or not: blocked,
12//! failed, stale or parked alike - and nothing is driving it, its worktree
13//! (and only its worktree) is released before the review starts. The
14//! branch, every commit and any pull request stay exactly where they were.
15//!
16//! A branch held by a run of *another* task is released on the same terms when
17//! magi can prove the worktree is its own: the path is a candidate worktree
18//! one run's record names, laid under that record's own worktree root, and the
19//! run is not being driven. Anything else - a path magi did not make, a live
20//! run, uncommitted files - is left alone and the refusal names which.
21//!
22//! [`decide`] is pure; [`release`] is the only function here that touches git
23//! or disk. A hand-run `magi review` has no task, so it carries a [`Takeover`]
24//! with no earlier attempts and can only release a worktree of a run magi
25//! itself recorded.
26
27use std::path::{Path, PathBuf};
28
29use anyhow::Result;
30
31use crate::git;
32use crate::run::{Liveness, RunState, RunStatus};
33
34/// The earlier attempts at a task a new run may take a branch over from.
35#[derive(Debug, Clone)]
36pub struct Takeover {
37    /// Ids of runs the new attempt supersedes, from
38    /// [`crate::queue::Task::earlier_attempts`].
39    pub earlier: Vec<String>,
40    /// The magi home their records live under.
41    pub home: PathBuf,
42    /// The owner's answer to an earlier divergence question about the branch,
43    /// applied once the earlier worktree is released (git refuses to move a
44    /// branch that is checked out).
45    pub choice: Option<crate::reconcile::Choice>,
46    /// The task's own statement. Used instead of the commit-log stand-in when
47    /// the earlier attempt's evidence is inherited, so the completion
48    /// criteria survive the retry.
49    pub task_instruction: Option<String>,
50}
51
52/// The operator has to decide: the takeover was refused, and the text says why.
53///
54/// A distinct type so the queue loop can hold the task for a person instead
55/// of spending an attempt on it (see `daemon::attempt`).
56///
57/// Structured so the operator-facing hold reason can be rendered in the run's
58/// language (`daemon::refused_text`); `Display` is the English text, unchanged.
59#[derive(Debug, Clone, PartialEq, Eq)]
60pub enum Refused {
61    /// The branch is held by a path magi cannot prove it made.
62    Foreign {
63        /// The contested branch.
64        branch: String,
65        /// The worktree that holds it.
66        path: String,
67        /// Machine detail of why it was refused (English).
68        why: String,
69    },
70    /// A magi worktree holds the branch, and releasing it is not safe.
71    Unsafe {
72        /// The contested branch.
73        branch: String,
74        /// The worktree that holds it.
75        path: String,
76        /// Machine detail of why it was refused (English).
77        why: String,
78    },
79    /// Releasing the earlier run's worktree failed and was undone.
80    ReleaseFailed {
81        /// The contested branch.
82        branch: String,
83        /// The worktree that holds it.
84        path: String,
85        /// Short id of the run that owned the worktree.
86        run: String,
87    },
88}
89
90impl std::fmt::Display for Refused {
91    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
92        match self {
93            Refused::Foreign { branch, path, why } => write!(
94                f,
95                "branch `{branch}` is checked out in {path}, which magi will not remove by \
96                 itself: {why}. Remove that worktree (`git worktree remove`) if it is not \
97                 needed, and try again."
98            ),
99            Refused::Unsafe { branch, path, why } => write!(
100                f,
101                "branch `{branch}` is checked out in {path}: {why}. Commit or discard the work \
102                 there and remove that worktree (`git worktree remove`), or say the run may be \
103                 discarded, and try again."
104            ),
105            Refused::ReleaseFailed { branch, path, run } => write!(
106                f,
107                "branch `{branch}` is checked out in {path} by run {run}, and releasing that \
108                 worktree failed or found it changed (git refuses to remove a worktree with \
109                 uncommitted changes); it was left as it was"
110            ),
111        }
112    }
113}
114
115impl std::error::Error for Refused {}
116
117/// A worktree that was released, with what is needed to put it back if the
118/// review that took the branch over never starts.
119#[derive(Debug, Clone)]
120pub struct Released {
121    /// The run whose worktree was released.
122    pub old_id: String,
123    index: usize,
124    path: PathBuf,
125    home: PathBuf,
126    successor: String,
127    /// The branch tip when the worktree was released; the branch sync may
128    /// have moved it since.
129    tip: String,
130    /// What was released and why it was safe, for the new run's events.
131    pub audit: String,
132}
133
134impl Released {
135    /// Undo the release after the new run failed to start: check the branch
136    /// out again at the old path and unmark the old run. Best-effort - if the
137    /// worktree cannot be re-added the old run stays marked, which is true.
138    pub async fn restore(&self, repo: &Path, branch: &str) {
139        let Ok(_owner) = crate::pr_head::owner_lock(&self.home, &self.old_id) else {
140            tracing::warn!("could not lock run {} for handover rollback", self.old_id);
141            return;
142        };
143        let Ok(mut state) = RunState::load_under(&self.old_id, &self.home) else {
144            return;
145        };
146        let claimed =
147            crate::daemon::is_working_on_other(&self.home, &self.old_id, jiff::Timestamp::now())
148                .unwrap_or(true);
149        if state.released_to.as_deref() != Some(self.successor.as_str())
150            || state.status == RunStatus::Superseded
151            || state.pr_taken_over_by.is_some()
152            || state.liveness_under(&self.home, claimed) != Liveness::Dead
153        {
154            tracing::warn!(
155                "run {} changed ownership; handover rollback refused",
156                self.old_id
157            );
158            return;
159        }
160        let path = self.path.to_string_lossy().to_string();
161        // Nothing holds the branch now, so it can be put back where the old
162        // run left it before its worktree is recreated at that commit.
163        let refname = format!("refs/heads/{branch}");
164        if git::rev_parse(repo, &refname).await.ok().as_deref() != Some(self.tip.as_str())
165            && let Err(e) = git::git(repo, &["branch", "-f", branch, &self.tip]).await
166        {
167            tracing::warn!("could not put `{branch}` back at {}: {e:#}", self.tip);
168            return;
169        }
170        if let Err(e) = git::git(repo, &["worktree", "add", &path, branch]).await {
171            tracing::warn!(
172                "could not put run {}'s worktree back at {path}: {e:#}",
173                self.old_id
174            );
175            return;
176        }
177        if let Some(c) = state.candidates.get_mut(self.index) {
178            c.folded = false;
179        }
180        state.released_to = None;
181        state.released_branches.retain(|b| b != branch);
182        state.events.pop();
183        let put_back = state.save_under(&self.home);
184        if let Err(e) = put_back {
185            tracing::warn!("could not unmark run {}: {e:#}", self.old_id);
186        }
187    }
188}
189
190/// What is known about the run and worktree holding the branch.
191#[derive(Debug, Clone)]
192pub struct Holder {
193    /// The run's id.
194    pub run: String,
195    /// Where the run got to.
196    pub status: RunStatus,
197    /// Whether anything is driving it.
198    pub liveness: Liveness,
199    /// Driver ownership could not be shown dead, including legacy records
200    /// without a pid: "unknown" must not read as "gone".
201    pub driver_unproven: bool,
202    /// Any uncommitted change, untracked files included.
203    pub dirty: bool,
204    /// The first few changed paths, for the refusal text.
205    pub dirty_files: Vec<String>,
206    /// The recorded driver pid, for the refusal text.
207    pub driver_pid: Option<u32>,
208    /// HEAD of the worktree.
209    pub head: String,
210    /// The branch tip in the repository.
211    pub tip: String,
212}
213
214/// Whose worktree holds the branch, as far as magi can prove.
215#[derive(Debug, Clone, PartialEq, Eq)]
216pub enum Owner {
217    /// A candidate worktree of an earlier attempt at the same task.
218    EarlierAttempt,
219    /// A candidate worktree some run's record names, laid under that run's
220    /// own worktree root: magi made it.
221    MagiRun,
222    /// Not provably magi's; the text says why.
223    Foreign(String),
224}
225
226/// The verdict of [`decide`].
227#[derive(Debug, Clone, PartialEq, Eq)]
228pub enum Decision {
229    /// Safe to release the worktree.
230    Release,
231    /// Not safe to release; the text says exactly why, for the operator.
232    Refuse(String),
233}
234
235fn short(sha: &str) -> String {
236    sha.chars().take(7).collect()
237}
238
239/// Whether `holder`'s worktree may be released to a new run.
240///
241/// [`Liveness::Unknown`] is refused like a live driver: an owner's answer
242/// cannot substitute for execution-lease or process-identity evidence. A run
243/// in `landing` (possibly waiting on the owner's approval) is refused as well.
244pub fn decide(owner: &Owner, holder: &Holder) -> Decision {
245    if let Owner::Foreign(why) = owner {
246        return Decision::Refuse(why.clone());
247    }
248    let mut why = Vec::new();
249    if holder.liveness == Liveness::Live {
250        why.push(format!(
251            "run {} is being worked on right now{}",
252            crate::run::short_of(&holder.run),
253            holder
254                .driver_pid
255                .map(|p| format!(" (driver pid {p})"))
256                .unwrap_or_default()
257        ));
258    } else if holder.liveness == Liveness::Unknown || holder.driver_unproven {
259        why.push("its driver process could not be shown to be gone".to_owned());
260    }
261    if holder.status == RunStatus::Landing {
262        why.push("that run is in `landing`, possibly waiting on an approval".to_owned());
263    }
264    if holder.dirty {
265        let files = holder.dirty_files.join(", ");
266        why.push(format!("its worktree has uncommitted changes ({files})"));
267    }
268    if holder.head != holder.tip {
269        why.push("its HEAD is not at the branch tip".to_owned());
270    }
271    if why.is_empty() {
272        return Decision::Release;
273    }
274    let kind = match owner {
275        Owner::EarlierAttempt => "an earlier attempt at this task",
276        _ => "a magi run of another task",
277    };
278    Decision::Refuse(format!(
279        "run {} (status `{}`, worktree {}, HEAD {}, branch tip {}) is {kind} and still has the \
280         branch checked out, so it was not released automatically: {}",
281        crate::run::short_of(&holder.run),
282        holder.status.as_str(),
283        if holder.dirty { "dirty" } else { "clean" },
284        short(&holder.head),
285        short(&holder.tip),
286        why.join("; ")
287    ))
288}
289
290/// Read what [`decide`] needs from disk and git.
291async fn inspect(
292    repo: &Path,
293    branch: &str,
294    path: &Path,
295    state: &RunState,
296    home: &Path,
297) -> Result<Holder> {
298    let claimed = crate::daemon::is_working_on_other(home, &state.id, jiff::Timestamp::now())?;
299    let liveness = state.liveness_under(home, claimed);
300    // Spelled out: `status.showUntrackedFiles=no` in a user's config would
301    // otherwise hide new files from `git status` and let them be deleted.
302    let porcelain = git::git(path, &["status", "--porcelain", "--untracked-files=normal"]).await?;
303    Ok(Holder {
304        run: state.id.clone(),
305        status: state.status,
306        liveness,
307        driver_unproven: liveness == Liveness::Unknown,
308        dirty: !porcelain.trim().is_empty(),
309        dirty_files: porcelain
310            .lines()
311            .take(5)
312            .map(|l| l.get(3..).unwrap_or(l).trim().to_owned())
313            .collect(),
314        driver_pid: state.driver_pid,
315        head: git::rev_parse(path, "HEAD").await?,
316        tip: git::rev_parse(repo, &format!("refs/heads/{branch}")).await?,
317    })
318}
319
320fn same_path(a: &Path, b: &Path) -> bool {
321    let canon = |p: &Path| std::fs::canonicalize(p).unwrap_or_else(|_| p.to_path_buf());
322    canon(a) == canon(b)
323}
324
325fn foreign(branch: &str, path: &Path, why: &str) -> anyhow::Error {
326    Refused::Foreign {
327        branch: branch.to_owned(),
328        path: path.display().to_string(),
329        why: why.to_owned(),
330    }
331    .into()
332}
333
334/// The run and candidate whose recorded worktree is `path`, when magi
335/// provably made it: the record names the path, the path sits directly in the
336/// record's own worktree root (`<root>/<short>/<dir>`), and exactly one run
337/// says so. `Err` is the reason a path under a root could not be proven.
338fn find_magi_owner(
339    path: &Path,
340    home: &Path,
341) -> std::result::Result<Option<(RunState, usize)>, String> {
342    let Some(bay) = path.parent() else {
343        return Ok(None);
344    };
345    let Some(bay_name) = bay.file_name().and_then(|n| n.to_str()) else {
346        return Ok(None);
347    };
348    let mut found = Vec::new();
349    for id in crate::run::list_ids_in(&home.join("runs")) {
350        if crate::run::short_of(&id) != bay_name {
351            continue;
352        }
353        let Ok(state) = RunState::load_under(&id, home) else {
354            return Err(format!("run {bay_name}'s record could not be read"));
355        };
356        if !same_path(&state.worktree_root(), bay) {
357            continue;
358        }
359        if let Some(i) = state
360            .candidates
361            .iter()
362            .position(|c| !c.folded && same_path(&c.worktree, path))
363        {
364            found.push((state, i));
365        }
366    }
367    match found.len() {
368        0 => Ok(None),
369        1 => Ok(found.pop()),
370        _ => Err(format!("more than one run record claims it ({bay_name})")),
371    }
372}
373
374/// A structured PR record survives both stop messages replacing `merge.detail`
375/// and older resumes accidentally resetting the graph status to Gating.
376pub(crate) fn landing_checkpoint(state: &RunState) -> bool {
377    state.status.resumable()
378        && state.config.graph.land
379        && state.pr.as_ref().is_some_and(|pr| pr.state == "open")
380        && state
381            .merge
382            .as_ref()
383            .is_some_and(|m| m.mode == crate::config::MergeMode::Pr && !m.empty)
384}
385
386/// Shared admission policy for queue retries, typed actions and control resume.
387/// Review exhaustion does not spend a publication or landing checkpoint, or
388/// prevent pending review verification or gate work from retrying an
389/// infrastructure wait.
390pub(crate) fn can_resume(state: &RunState) -> bool {
391    let review = crate::graph::review_conclusion(&state.reviews, state.config.graph.review_rounds);
392    state.status.resumable()
393        && !state.released()
394        && state.retired_to.is_none()
395        && state.pr_taken_over_by.is_none()
396        && (resume_checkpoint(state)
397            || crate::run::try_home().is_some_and(|home| has_resume_checkpoint(state, &home))
398            || ((state.config.graph.review_rounds == 0
399                || state.reviews.iter().any(|round| round.clean))
400                && (!state.gate_ran || state.resource_block.is_some()))
401            || (state.resource_block.is_some() && review == Some(RunStatus::Gating))
402            || (review.is_none()
403                && state.reviews.last().is_some_and(|round| {
404                    round.e2e_status() == crate::run::E2eStatus::ResourceBlocked
405                }))
406            || !(state.status == RunStatus::Blocked
407                && state.reviews.len() >= state.config.graph.review_rounds))
408}
409
410fn resume_checkpoint(state: &RunState) -> bool {
411    state.status == RunStatus::Landing
412        || landing_checkpoint(state)
413        || state.pr_publication_pending
414        || state.land_approval.is_some()
415        || state.merge_decision.is_some()
416        || state.land_armed_head.is_some()
417        || !state.merge_repairs.is_empty()
418}
419
420/// A resumable publication, landing or owner decision must keep its existing
421/// graph and worktree. Call under the owner lock before releasing or retiring
422/// a predecessor; completed runs may retain these fields as historical evidence.
423pub(crate) fn has_resume_checkpoint(state: &RunState, home: &Path) -> bool {
424    state.status.resumable()
425        && (resume_checkpoint(state)
426            || crate::ask::Questions::at(home.join("questions"))
427                .list()
428                .iter()
429                .any(|q| q.run == state.id && q.status.open()))
430}
431
432/// The scan only finds a possible holder. Driver claims share this lock, so
433/// reload and validate the candidate while holding it through removal/rollback.
434fn locked_holder(
435    home: &Path,
436    id: &str,
437    path: &Path,
438) -> Result<(RunState, usize, crate::pr_head::OwnerLock)> {
439    let owner = crate::pr_head::owner_lock(home, id)?;
440    let state = RunState::load_under(id, home)?;
441    let index = state
442        .candidates
443        .iter()
444        .position(|c| !c.folded && same_path(&c.worktree, path))
445        .ok_or_else(|| anyhow::anyhow!("worktree holder changed; retry takeover"))?;
446    anyhow::ensure!(
447        state.released_to.is_none(),
448        "run already handed over its worktree; retry takeover"
449    );
450    anyhow::ensure!(
451        path.parent()
452            .is_some_and(|p| same_path(&state.worktree_root(), p)),
453        "worktree holder root changed; retry takeover"
454    );
455    Ok((state, index, owner))
456}
457
458/// Release the worktree holding `branch` to `new_run`, when an earlier attempt
459/// at the same task, or a run magi itself recorded, holds it and it is safe. Returns the run whose worktree
460/// was released (see [`Released`]), or `None` when nothing needed (or was allowed) to happen.
461///
462/// An `Err` is a refusal or a failed removal; the message is what the
463/// operator reads. Nothing is left half-done: the old run's record is written
464/// before the removal and rolled back if git refuses it.
465pub async fn release(
466    repo: &Path,
467    branch: &str,
468    new_run: &str,
469    takeover: &Takeover,
470) -> Result<Option<Released>> {
471    let Some(path) = git::worktree_holding(repo, branch).await? else {
472        return Ok(None);
473    };
474    // The holder has to be an unfolded candidate worktree of a run on record.
475    // One nobody recorded (made by hand, say) is not ours to touch.
476    let mut owner = None;
477    for id in &takeover.earlier {
478        let Ok(state) = RunState::load_under(id, &takeover.home) else {
479            continue;
480        };
481        if let Some(i) = state
482            .candidates
483            .iter()
484            .position(|c| !c.folded && same_path(&c.worktree, &path))
485        {
486            // Naming the path is not enough: magi must have laid it too.
487            if !path
488                .parent()
489                .is_some_and(|b| same_path(&state.worktree_root(), b))
490            {
491                return Err(foreign(
492                    branch,
493                    &path,
494                    &format!(
495                        "run {} records it, but it is outside that run's worktree root, so \
496                         magi did not make it",
497                        crate::run::short_of(id)
498                    ),
499                ));
500            }
501            owner = Some((state, i, Owner::EarlierAttempt));
502            break;
503        }
504    }
505    if owner.is_none() {
506        owner = match find_magi_owner(&path, &takeover.home) {
507            Ok(found) => found.map(|(s, i)| (s, i, Owner::MagiRun)),
508            Err(why) => return Err(foreign(branch, &path, &why)),
509        };
510    }
511    let Some((state, _, kind)) = owner else {
512        return Err(foreign(
513            branch,
514            &path,
515            &format!(
516                "{} is not a candidate worktree any magi run recorded (made by hand, or by \
517                 something other than magi)",
518                path.display()
519            ),
520        ));
521    };
522
523    let (mut state, index, _owner) = locked_holder(&takeover.home, &state.id, &path)?;
524    if has_resume_checkpoint(&state, &takeover.home) {
525        return Err(Refused::Unsafe {
526            branch: branch.to_owned(),
527            path: path.display().to_string(),
528            why: format!("run {} has a resumable publication, landing or decision checkpoint; resume that run instead of releasing its worktree", state.short()),
529        }.into());
530    }
531    let holder = inspect(repo, branch, &path, &state, &takeover.home).await?;
532    if let Decision::Refuse(why) = decide(&kind, &holder) {
533        return Err(Refused::Unsafe {
534            branch: branch.to_owned(),
535            path: path.display().to_string(),
536            why,
537        }
538        .into());
539    }
540    let audit = format!(
541        "run {} (status `{}`, no driver, clean, HEAD {} = branch tip, branch `{branch}` kept)",
542        crate::run::short_of(&holder.run),
543        holder.status.as_str(),
544        short(&holder.head)
545    );
546
547    let old_id = state.id.clone();
548    state.candidates[index].folded = true;
549    state.released_to = Some(new_run.to_owned());
550    if !state.released_branches.iter().any(|b| b == branch) {
551        state.released_branches.push(branch.to_owned());
552    }
553    state.event(
554        "release",
555        format!(
556            "worktree of `{branch}` released to run {}: {audit}; this run can no longer be \
557             resumed from here",
558            crate::run::short_of(new_run)
559        ),
560    );
561    state.save_under(&takeover.home)?;
562
563    // Re-read right before the removal for the driver and the tip, and let git
564    // itself refuse a dirty worktree: the removal is *not* `--force`, so an
565    // edit made after the first look is refused rather than thrown away.
566    // Ignored files such as `target/` go with the worktree by design.
567    // Against the record as it is on disk now, not the copy read earlier: a
568    // driver cannot claim between this check and removal: _owner remains
569    // held through the save, removal and any rollback.
570    let again = match RunState::load_under(&old_id, &takeover.home) {
571        Ok(fresh) => inspect(repo, branch, &path, &fresh, &takeover.home).await,
572        Err(e) => Err(e),
573    };
574    let safe = matches!(&again, Ok(h) if decide(&kind, h) == Decision::Release);
575    let removed = safe
576        && git::worktree_remove_clean(repo, &path)
577            .await
578            .unwrap_or(false);
579    if !removed || path.exists() {
580        state = RunState::load_under(&old_id, &takeover.home)?;
581        state.candidates[index].folded = false;
582        state.released_to = None;
583        state.released_branches.retain(|b| b != branch);
584        state.events.pop();
585        state.event(
586            "release",
587            format!(
588                "release of `{branch}` to run {} was undone: {audit}",
589                crate::run::short_of(new_run)
590            ),
591        );
592        state.save_under(&takeover.home)?;
593        return Err(Refused::ReleaseFailed {
594            branch: branch.to_owned(),
595            path: path.display().to_string(),
596            run: crate::run::short_of(&old_id).to_owned(),
597        }
598        .into());
599    }
600    Ok(Some(Released {
601        old_id,
602        index,
603        path,
604        home: takeover.home.clone(),
605        successor: new_run.to_owned(),
606        tip: holder.tip,
607        audit,
608    }))
609}
610
611#[cfg(test)]
612mod tests {
613    #[test]
614    fn refused_display_is_the_english_text() {
615        let r = Refused::Foreign {
616            branch: "b".into(),
617            path: "/w".into(),
618            why: "w".into(),
619        };
620        assert_eq!(
621            r.to_string(),
622            "branch `b` is checked out in /w, which magi will not remove by itself: w. Remove \
623             that worktree (`git worktree remove`) if it is not needed, and try again."
624        );
625    }
626
627    use super::*;
628
629    fn holder() -> Holder {
630        Holder {
631            run: "20260901-000000-f82f".to_owned(),
632            status: RunStatus::Gating,
633            liveness: Liveness::Dead,
634            driver_unproven: false,
635            dirty: false,
636            dirty_files: Vec::new(),
637            driver_pid: None,
638            head: "a".repeat(40),
639            tip: "a".repeat(40),
640        }
641    }
642
643    fn recorded_holder(home: &Path) -> (RunState, PathBuf) {
644        let mut state = RunState::new(
645            "repo".into(),
646            "main".into(),
647            "base".into(),
648            "task".into(),
649            crate::config::Config::default(),
650        );
651        state.config.graph.worktree_root = Some(home.join("worktrees"));
652        let path = state.worktree_root().join("cand-A");
653        std::fs::create_dir_all(&path).unwrap();
654        state.driver_exited = true;
655        state.candidates.push(crate::run::Candidate {
656            index: 0,
657            label: 'A',
658            agent: "existing".into(),
659            branch: "branch".into(),
660            worktree: path.clone(),
661            summary: String::new(),
662            stat: String::new(),
663            files: 1,
664            commits: 1,
665            empty: false,
666            failed: None,
667            verified_noop: None,
668            duration_ms: 0,
669            folded: false,
670        });
671        state.save_under(home).unwrap();
672        (state, path)
673    }
674
675    #[test]
676    fn handover_reloads_a_driver_claim_after_the_scan_and_excludes_new_claims() {
677        let home = tempfile::tempdir().unwrap();
678        let (mut state, path) = recorded_holder(home.path());
679        let scanned = state.clone();
680        // A resume won the race after the holder scan, before the owner lock.
681        state.driver_exited = false;
682        state.driver_pid = Some(std::process::id());
683        state.driver_started_at = crate::proc::process_started_at(std::process::id());
684        state.begin_pass();
685        state.save_under(home.path()).unwrap();
686        let mut driver = crate::driver::DriverGuard::acquire(home.path(), &state).unwrap();
687        driver.record(&state).unwrap();
688        let (fresh, _, owner) = locked_holder(home.path(), &scanned.id, &path).unwrap();
689        assert!(!fresh.driver_exited);
690        assert_eq!(fresh.passes, state.passes);
691        assert_ne!(fresh.liveness_under(home.path(), false), Liveness::Dead);
692        assert!(crate::pr_head::owner_lock(home.path(), &state.id).is_err());
693        assert!(path.exists());
694        assert!(
695            RunState::load_under(&state.id, home.path())
696                .unwrap()
697                .released_to
698                .is_none()
699        );
700        drop(owner);
701        assert!(crate::pr_head::owner_lock(home.path(), &state.id).is_ok());
702    }
703
704    #[test]
705    fn handover_revalidates_candidate_ownership_under_the_claim_lock() {
706        let home = tempfile::tempdir().unwrap();
707        let (mut state, path) = recorded_holder(home.path());
708        state.candidates[0].folded = true;
709        state.released_to = Some("next".into());
710        state.save_under(home.path()).unwrap();
711        assert!(locked_holder(home.path(), &state.id, &path).is_err());
712        assert!(path.exists());
713    }
714
715    #[tokio::test]
716    async fn a_stale_restore_does_not_undo_a_later_handover() {
717        let home = tempfile::tempdir().unwrap();
718        let (mut state, path) = recorded_holder(home.path());
719        state.candidates[0].folded = true;
720        state.released_to = Some("later".into());
721        state.save_under(home.path()).unwrap();
722        let released = Released {
723            old_id: state.id.clone(),
724            index: 0,
725            path,
726            home: home.path().to_owned(),
727            successor: "original".into(),
728            tip: "tip".into(),
729            audit: String::new(),
730        };
731        released.restore(Path::new("nonexistent"), "branch").await;
732        let disk = RunState::load_under(&state.id, home.path()).unwrap();
733        assert_eq!(disk.released_to.as_deref(), Some("later"));
734        assert!(disk.candidates[0].folded);
735    }
736
737    #[test]
738    fn a_clean_superseded_stale_run_is_released() {
739        assert_eq!(decide(&Owner::EarlierAttempt, &holder()), Decision::Release);
740        let dead = Holder {
741            liveness: Liveness::Dead,
742            ..holder()
743        };
744        assert_eq!(decide(&Owner::EarlierAttempt, &dead), Decision::Release);
745    }
746
747    #[test]
748    fn a_foreign_worktree_is_refused_with_its_reason() {
749        let owner = Owner::Foreign("it is a foreign path".to_owned());
750        assert_eq!(
751            decide(&owner, &holder()),
752            Decision::Refuse("it is a foreign path".to_owned())
753        );
754    }
755
756    #[test]
757    fn landing_is_protected_even_for_an_earlier_attempt() {
758        let landing = Holder {
759            status: RunStatus::Landing,
760            ..holder()
761        };
762        assert!(matches!(
763            decide(&Owner::MagiRun, &landing),
764            Decision::Refuse(_)
765        ));
766        assert!(matches!(
767            decide(&Owner::EarlierAttempt, &landing),
768            Decision::Refuse(_)
769        ));
770    }
771
772    #[test]
773    fn a_dirty_worktree_is_refused_and_says_why() {
774        let dirty = Holder {
775            dirty: true,
776            dirty_files: vec!["scratch.txt".to_owned()],
777            ..holder()
778        };
779        let Decision::Refuse(why) = decide(&Owner::EarlierAttempt, &dirty) else {
780            panic!("dirty must be refused");
781        };
782        assert!(
783            why.contains("uncommitted") && why.contains("scratch.txt"),
784            "{why}"
785        );
786        assert!(why.contains("f82f") && why.contains("gating") && why.contains("dirty"));
787    }
788
789    #[test]
790    fn a_live_run_is_refused() {
791        let live = Holder {
792            liveness: Liveness::Live,
793            driver_pid: Some(4242),
794            ..holder()
795        };
796        let Decision::Refuse(why) = decide(&Owner::EarlierAttempt, &live) else {
797            panic!("live must be refused");
798        };
799        assert!(
800            why.contains("right now") && why.contains("f82f") && why.contains("4242"),
801            "{why}"
802        );
803    }
804
805    #[test]
806    fn unknown_without_a_recorded_pid_is_not_proof_of_a_stopped_driver() {
807        let holder = Holder {
808            liveness: Liveness::Unknown,
809            ..holder()
810        };
811        assert!(matches!(
812            decide(&Owner::EarlierAttempt, &holder),
813            Decision::Refuse(_)
814        ));
815    }
816
817    #[test]
818    fn a_driver_that_could_not_be_shown_dead_is_refused() {
819        let unproven = Holder {
820            driver_unproven: true,
821            ..holder()
822        };
823        let Decision::Refuse(why) = decide(&Owner::EarlierAttempt, &unproven) else {
824            panic!("an unproven driver must be refused");
825        };
826        assert!(why.contains("driver"), "{why}");
827    }
828
829    #[test]
830    fn a_head_off_the_tip_is_refused() {
831        let off = Holder {
832            head: "b".repeat(40),
833            ..holder()
834        };
835        assert!(matches!(
836            decide(&Owner::EarlierAttempt, &off),
837            Decision::Refuse(_)
838        ));
839    }
840}