cosca 0.7.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
"""macOS guest backend for devvm.py via Tart (https://tart.run); see scripts/README.md#macos-arm64.

The base image is Cirrus Labs' `macos-tahoe-base`, not GitHub's runner image: CI stays the merge gate.
"""

from __future__ import annotations

import argparse
import contextlib
import fcntl
import functools
import json
import os
import select
import shlex
import shutil
import signal
import subprocess
import sys
import threading
import time
import uuid
from pathlib import Path

GUEST_NAME = "macos-arm64"
BASE_IMAGE_REPO = "ghcr.io/cirruslabs/macos-tahoe-base"
BASE_IMAGE_TAG = "latest"
BASE_IMAGE_DIGEST = "sha256:1b093499716409d29e8b5336844528e1cae375db97d2ad8e5aeff78cf0da201e"
BASE_IMAGE = f"{BASE_IMAGE_REPO}@{BASE_IMAGE_DIGEST}"
# Failure bound on guest boot (an external event), reported to the human.
AGENT_BOOT_TIMEOUT_SECONDS = 600
VM_PREFIX = "devvm-macos-"
MAX_CONCURRENT_VMS = 2
GUEST_TREE = "~/cosca"
CHECKPOINTS = ("start", "claimed", "locked", "counted", "cloned", "booted", "unlocked", "archived", "provisioned")
PROVISION_SCRIPT = "scripts/devvm/provision/macos-rust.sh"
# Read by .github/scripts/unattended-gui-elevation.py; written only by this driver, inside a guest.
GUEST_MARKER = "/etc/cosca-devvm-guest"
# Guest side of `fetch`: expands a leading `~/` in the guest, then tars the path. The `./` keeps the
# name from being read as an option (`-x`) or, by bsdtar, as "copy entries from this archive" (`@x`).
FETCH_SCRIPT = (
    'p=$1; case "$p" in "~/"*) p="$HOME/${p#"~/"}";; esac; '
    'p=${p%/}; exec tar -c -C "$(dirname "$p")" "./$(basename "$p")"'
)


class CapExceeded(RuntimeError):
    pass


class NameTaken(RuntimeError):
    pass


class BadRev(ValueError):
    pass


class AlreadyUp(RuntimeError):
    pass


class UpError(RuntimeError):
    """A failure `up` reports to the human after cleaning up."""


# Pure helpers =========================================================================


def tart_bin() -> str:
    """The tart executable: $TART if set, else `tart` on PATH."""
    explicit = os.environ.get("TART")
    if explicit:
        if not Path(explicit).is_file():
            raise RuntimeError(f"$TART is set to '{explicit}', which is not a file")
        return explicit
    found = shutil.which("tart")
    if found is None:
        raise RuntimeError("tart not found: put it on PATH or set $TART to its path. See scripts/README.md#macos-arm64.")
    return found


def tart_home() -> Path:
    return Path(os.environ.get("TART_HOME") or Path.home() / ".tart")


def count_running(vms: list[dict]) -> int:
    return sum(
        1
        for e in vms
        if e.get("Source") == "local" and (e.get("State") == "running" or e.get("Running") is True)
    )


def check_cap(running: int) -> None:
    if running >= MAX_CONCURRENT_VMS:
        raise CapExceeded(
            f"{running} Tart VMs are already running on this Mac (every local VM is counted, Linux ones "
            f"included); Apple's licence allows at most {MAX_CONCURRENT_VMS} macOS VMs concurrently. "
            "Destroy one first (`devvm.py destroy macos-arm64` in the worktree that owns it, or `tart list`)."
        )


def new_vm_name() -> str:
    return f"{VM_PREFIX}{uuid.uuid4().hex}"


def require_name_free(name: str, vms: list[dict]) -> None:
    if name in {e.get("Name") for e in vms}:
        raise NameTaken(f"a Tart VM named '{name}' already exists; refusing to clone over it")


def require_own_name(name: str) -> None:
    """Never delete or stop anything this tool did not create (the base image included)."""
    if not name.startswith(VM_PREFIX):
        raise ValueError(f"refusing to touch '{name}': not a devvm-created VM (must start with '{VM_PREFIX}')")


def resolve_rev(repo_root: Path, rev: str) -> str:
    """The full commit sha for rev; `--end-of-options` keeps an option-looking rev from being a git flag."""
    r = subprocess.run(
        ["git", "-C", str(repo_root), "rev-parse", "--verify", "--end-of-options", f"{rev}^{{commit}}"],
        capture_output=True,
        text=True,
    )
    if r.returncode != 0:
        raise BadRev(f"'{rev}' is not a commit in {repo_root}")
    return r.stdout.strip()


def identity_matches(recorded: dict, current: dict) -> bool:
    keys = ("ino", "dev")
    return all(k in recorded and k in current and recorded[k] == current[k] for k in keys)


def build_remote_command(cmd_args: list[str]) -> str:
    return (
        'export PATH="$HOME/.cargo/bin:$PATH"; '
        f"cd {GUEST_TREE} && CARGO_TARGET_DIR=$HOME/cargo-target {shlex.join(cmd_args)}"
    )


def parse_guest_path(path: str) -> str:
    """Validate a fetch source: absolute, or `~/`-relative (expanded in the guest)."""
    if not (path.startswith("/") or path.startswith("~/")):
        raise ValueError(f"fetch needs an absolute path (or ~/...), got '{path}'; quote ~ so the host shell leaves it")
    target = path[2:] if path.startswith("~/") else path
    if os.path.basename(target.rstrip("/")) in ("", ".", ".."):
        raise ValueError(f"'{path}' names no file or directory")
    return path


def _write_atomic(path: Path, text: str) -> None:
    tmp = path.with_name(f"{path.name}.{uuid.uuid4().hex}.tmp")
    try:
        with open(tmp, "w") as f:
            f.write(text)
            f.flush()
            os.fsync(f.fileno())
        os.replace(tmp, path)
    except BaseException:
        tmp.unlink(missing_ok=True)
        raise


def _say(message: str, *, file=None) -> None:
    """print() that cannot fail: a closed pipe or a hung-up terminal must not break cleanup or the exit code."""
    file = file if file is not None else sys.stderr
    try:
        print(message, file=file)
        file.flush()
    except (OSError, ValueError):
        # Point the stream at /dev/null, so neither later prints nor the flush at interpreter exit fail.
        null = open(os.devnull, "w")  # noqa: SIM115  lives as long as the process
        if file is sys.stderr:
            sys.stderr = null
        elif file is sys.stdout:
            sys.stdout = null


def _describe(error: Exception) -> str:
    """The message for a failure: our own RuntimeError/ValueError texts are written for the user as they are."""
    return str(error) if isinstance(error, (RuntimeError, ValueError)) else f"{type(error).__name__}: {error}"


def _reports_failures(verb):
    """Any failure of a verb is a message and exit status 1, never a traceback."""

    @functools.wraps(verb)
    def wrapper(self, *args, **kwargs):
        try:
            return verb(self, *args, **kwargs)
        except Exception as e:  # noqa: BLE001
            _say(f"error: {_describe(e)}")
            sys.exit(1)

    return wrapper


class Cancelled(BaseException):
    """A signal asked `up` or `destroy` to stop. Raised by SignalGate's own code, never from a signal handler."""

    def __init__(self, signo: int):
        super().__init__(signal.Signals(signo).name)
        self.signo = signo


class SignalGate:
    """Turns SIGINT, SIGTERM and SIGHUP into data.

    The Python handlers do nothing; the signal number is written to a non-blocking self-pipe
    (`signal.set_wakeup_fd`). Nothing is ever raised from a handler, so a signal cannot unwind
    through library code (subprocess, locks) halfway. Every wait multiplexes on the pipe via
    `wait_for`, and `check` is polled between steps; both raise `Cancelled` from ordinary code.
    Signals the process inherited as ignored (`nohup`, a backgrounded non-interactive shell)
    stay ignored.
    """

    SIGNALS = (signal.SIGINT, signal.SIGTERM, signal.SIGHUP)
    on_blocked = None  # test seam: a staticmethod called just before every blocking select

    def __init__(self) -> None:
        self.pending: list[int] = []
        self.abandoned: list[threading.Thread] = []  # lock waiters left behind by a cancelled wait
        self._old: dict[int, object] = {}
        self._old_wakeup = -1
        self._r, self._w = os.pipe()
        os.set_blocking(self._r, False)
        os.set_blocking(self._w, False)

    def __enter__(self) -> SignalGate:
        self._old_wakeup = signal.set_wakeup_fd(self._w, warn_on_full_buffer=False)
        for sig in self.SIGNALS:
            if signal.getsignal(sig) is not signal.SIG_IGN:
                self._old[sig] = signal.signal(sig, lambda _signo, _frame: None)
        return self

    def __exit__(self, *_exc) -> None:
        # Handlers first: a later signal then takes its default action instead of being recorded
        # and dropped. Then one last drain, so `pending` holds every signal that was recorded.
        for sig, handler in self._old.items():
            signal.signal(sig, handler)
        self._drain()
        signal.set_wakeup_fd(self._old_wakeup)
        os.close(self._r)
        os.close(self._w)

    def _drain(self) -> None:
        while True:
            try:
                data = os.read(self._r, 64)
            except BlockingIOError:
                return
            if not data:
                return
            self.pending.extend(data)

    def check(self) -> None:
        self._drain()
        if self.pending:
            raise Cancelled(self.pending[0])

    def wait_for(self, fds: list[int], timeout: float | None = None) -> list[int]:
        """Block until one of `fds` is readable or `timeout` passes; raise `Cancelled` on a signal."""
        if self.on_blocked:
            self.on_blocked()
        self.check()
        ready, _, _ = select.select([self._r, *fds], [], [], timeout)
        self.check()
        return [fd for fd in ready if fd != self._r]

    def sleep(self, seconds: float) -> None:
        self.wait_for([], seconds)

    def run(self, argv: list[str], *, stdin=None, capture_output=False, text=False, **kw) -> subprocess.CompletedProcess:
        """subprocess.run that a signal can cut short: the child is killed and reaped, then `Cancelled` is raised.

        Exactly one party reaps. A helper thread only *observes* the exit (`waitid` with `WNOWAIT` leaves
        the zombie in place); the calling thread alone decides, kills if it must, and reaps. The pid is
        therefore still ours whenever it is signalled. `Popen.wait`, `poll` and `kill` are never used.
        """
        pipe = subprocess.PIPE if capture_output else None
        exited_r, exited_w = os.pipe()  # before the child exists: a failure here leaves nothing running
        try:
            proc = subprocess.Popen(argv, stdin=stdin, stdout=pipe, stderr=pipe, text=text, **kw)
        except BaseException:
            os.close(exited_r)
            os.close(exited_w)
            raise
        # From here on the child is ours to kill and reap, whatever fails next.
        pid = proc.pid
        streams = [st for st in (proc.stdout, proc.stderr) if st is not None]
        captured: dict[int, object] = {}

        def read(i: int, stream) -> None:
            captured[i] = stream.read()

        def observe() -> None:
            try:
                os.waitid(os.P_PID, pid, os.WEXITED | os.WNOWAIT)
            finally:
                os.write(exited_w, b"x")

        started: list[threading.Thread] = []
        interrupted: BaseException | None = None
        try:
            for target, args in (*((read, (i, st)) for i, st in enumerate(streams)), (observe, ())):
                thread = threading.Thread(target=target, args=args, daemon=True)
                thread.start()
                started.append(thread)
            self.wait_for([exited_r])
        except BaseException as e:  # noqa: BLE001  Cancelled, or anything else: the child must not outlive us
            interrupted = e
            os.kill(pid, signal.SIGKILL)  # not reaped yet, so the pid is still our child (alive or a zombie)
        for thread in started:
            thread.join()
        proc.returncode = os.waitstatus_to_exitcode(os.waitpid(pid, 0)[1])
        for st in streams:
            st.close()
        os.close(exited_r)
        os.close(exited_w)
        if interrupted is not None:
            raise interrupted
        return subprocess.CompletedProcess(argv, proc.returncode, captured.get(0), captured.get(1))

    def flock(self, f) -> None:
        """Take an exclusive flock on `f`, waiting in a helper thread so that a signal can end the wait.

        On `Cancelled` this has taken over `f`: a waiter that is still blocked closes it, releasing
        the lock, the moment it would have acquired it. A lock acquired just before the cancel is
        released here. Either way no lock outlives the cancelled wait.
        """
        mutex, state = threading.Lock(), {"s": "waiting", "error": None}
        done_r, done_w = os.pipe()

        def acquire() -> None:
            error = None
            try:
                fcntl.flock(f, fcntl.LOCK_EX)
            except BaseException as e:  # noqa: BLE001
                error = e
            with mutex:
                if state["s"] == "abandoned":
                    f.close()
                    os.close(done_w)  # nobody is waiting for the notification any more
                else:
                    state["s"], state["error"] = "done", error
                    os.write(done_w, b"x")  # the only wake-up: the pipe's write end stays open until the caller is done

        waiter = threading.Thread(target=acquire, daemon=True)
        waiter.start()
        try:
            self.wait_for([done_r])
        except BaseException:
            with mutex:
                if state["s"] == "done":
                    f.close()
                    os.close(done_w)
                else:
                    state["s"] = "abandoned"  # the waiter closes both `f` and `done_w` when it acquires
                    self.abandoned.append(waiter)
            os.close(done_r)
            raise
        os.close(done_r)
        waiter.join()
        os.close(done_w)
        if state["error"] is not None:
            f.close()
            raise state["error"]


@contextlib.contextmanager
def _flock(path: Path, waiting_for: str, gate: SignalGate | None = None):
    """Hold an exclusive flock; the wait can be ended by a signal only if a gate is given."""
    path.parent.mkdir(parents=True, exist_ok=True)
    f = open(path, "w")  # noqa: SIM115
    handed_off = False
    try:
        try:
            fcntl.flock(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
        except BlockingIOError:
            _say(f"devvm: waiting for {waiting_for}...")
            if gate is None:
                fcntl.flock(f, fcntl.LOCK_EX)
            else:
                handed_off = True  # on Cancelled the gate has taken over `f`
                gate.flock(f)
                handed_off = False
        yield
    finally:
        if not handed_off:
            f.close()


# Tart runner (the test seam) ==========================================================


class Tart:
    """Thin wrapper over the tart CLI; tests substitute a fake with the same methods.

    Methods that take `gate` wait through it, so a signal ends them (child killed, `Cancelled` raised).
    """

    def __init__(self, binary: str | None = None):
        self.binary = binary or tart_bin()
        self.home = tart_home()

    def _cmd(self, args: list[str]) -> list[str]:
        cmd = [self.binary, *args]
        _say(f"+ {shlex.join(cmd)}")
        return cmd

    @staticmethod
    def _run(argv: list[str], gate: SignalGate | None, **kw) -> subprocess.CompletedProcess:
        return gate.run(argv, **kw) if gate else subprocess.run(argv, **kw)

    def vms(self, gate: SignalGate | None = None) -> list[dict]:
        out = self._run(
            self._cmd(["list", "--format", "json"]),
            gate,
            capture_output=True,
            text=True,
            start_new_session=True,  # cleanup's children must not see a terminal Ctrl-C
        )
        out.check_returncode()
        return json.loads(out.stdout)

    def clone(self, src: str, name: str, gate: SignalGate | None = None) -> int:
        return self._run(self._cmd(["clone", src, name]), gate).returncode

    def start(self, name: str, log: Path) -> subprocess.Popen:
        with open(log, "wb") as logf:
            return subprocess.Popen(
                self._cmd(["run", "--no-graphics", name]),
                stdout=logf,
                stderr=subprocess.STDOUT,
                start_new_session=True,
            )

    def stop(self, name: str) -> int:
        return subprocess.run(self._cmd(["stop", name]), start_new_session=True).returncode

    def delete(self, name: str) -> int:
        return subprocess.run(self._cmd(["delete", name]), start_new_session=True).returncode

    def _exec_cmd(self, name: str, args: list[str], stdin) -> list[str]:
        return self._cmd(["exec", *(["-i"] if stdin is not None else []), name, *args])

    def exec(self, name, args, *, stdin=None, capture_output=False, gate=None) -> subprocess.CompletedProcess:
        return self._run(self._exec_cmd(name, args, stdin), gate, stdin=stdin, capture_output=capture_output)

    def exec_popen(self, name, args, *, stdout) -> subprocess.Popen:
        return subprocess.Popen(self._exec_cmd(name, args, None), stdout=stdout)

    def exec_interactive(self, name: str) -> None:
        cmd = self._cmd(["exec", "-i", "-t", name, "bash", "-l"])
        os.execvp(cmd[0], cmd)

    def identity(self, name: str) -> dict:
        """The VM's disk identity; raises FileNotFoundError if its disk.img is missing."""
        st = (self.home / "vms" / name / "disk.img").stat()
        return {"ino": st.st_ino, "dev": st.st_dev}


# Backend ==============================================================================


class MacosBackend:
    def __init__(self, tart: Tart | None, repo_root: Path, state_root: Path):
        self._tart = tart  # built on first use: `list` and `status` must work without tart installed
        self.repo_root = repo_root
        self.sdir = state_root / GUEST_NAME
        self._proc: subprocess.Popen | None = None  # the `tart run` child `up` owns
        self._gate: SignalGate | None = None
        self.last_gate: SignalGate | None = None  # for tests: the abandoned lock waiters
        self._claimed = self._clone_attempted = self._cleaned = self._done = False

    @property
    def tart(self) -> Tart:
        if self._tart is None:
            self._tart = Tart()
        return self._tart

    # state -----------------------------------------------------------------------

    @property
    def _claim_file(self) -> Path:
        return self.sdir / "vm_name"

    @property
    def _identity_file(self) -> Path:
        return self.sdir / "identity.json"

    def _read_claim(self) -> str | None:
        """The recorded VM name; None if there is no claim, "" if the claim is empty."""
        try:
            text = self._claim_file.read_text()
        except FileNotFoundError:
            return None
        parts = text.split()
        return parts[0] if parts else ""

    def _claim(self, name: str) -> None:
        self.sdir.mkdir(parents=True, exist_ok=True)
        tmp = self.sdir / f"vm_name.{uuid.uuid4().hex}.tmp"
        try:
            with open(tmp, "w") as f:
                f.write(name + "\n")
                f.flush()
                os.fsync(f.fileno())
            try:
                os.link(tmp, self._claim_file)
            except FileExistsError:
                raise AlreadyUp("guest 'macos-arm64' already exists for this worktree; `destroy` it first") from None
        finally:
            tmp.unlink(missing_ok=True)

    def _clear_state(self) -> None:
        for f in (self._claim_file, self._identity_file):
            f.unlink(missing_ok=True)

    def _worktree_lock(self, *, interruptible: bool):
        return _flock(self.sdir / "lock", "another devvm command in this worktree", self._gate if interruptible else None)

    def _cap_lock(self, *, interruptible: bool):
        return _flock(
            self.tart.home / "devvm-cap.lock",
            "the host-wide macOS VM lock (another `up` is running)",
            self._gate if interruptible else None,
        )

    def _pause(self, seconds: float) -> None:
        self._gate.sleep(seconds) if self._gate else time.sleep(seconds)

    def _checkpoint(self, label: str) -> None:
        assert label in CHECKPOINTS, label
        if self._gate:
            self._gate.check()

    def _require_vm(self) -> str:
        name = self._read_claim()
        if not name:
            _say("error: guest 'macos-arm64' has not been brought up; run `devvm.py up macos-arm64` first")
            sys.exit(1)
        require_own_name(name)
        return name

    # removal ---------------------------------------------------------------------

    def _names(self) -> set[str]:
        return {e.get("Name") for e in self.tart.vms()}

    def _remove_vm(self, name: str, proc: subprocess.Popen | None) -> str | None:
        """Stop and delete the VM, then confirm it is gone. Returns a problem description, or None."""
        require_own_name(name)
        if name in self._names():
            self.tart.stop(name)
            running = {e.get("Name") for e in self.tart.vms() if e.get("State") == "running" or e.get("Running") is True}
            if name in running:
                return f"`tart stop {name}` did not stop it"
        if proc is not None:
            if proc.poll() is None:
                proc.terminate()
            proc.wait()
        if name in self._names():
            rc = self.tart.delete(name)
            if rc != 0 or name in self._names():
                return f"`tart delete {name}` failed (exit {rc})"
        return None

    def _teardown(self, name: str) -> None:
        """Remove a VM `up` created. Never raises, so the original error survives; keeps the state on failure."""
        try:
            existed = name in self._names()
            problem = self._remove_vm(name, self._proc)
        except Exception as e:  # noqa: BLE001
            existed, problem = True, repr(e)
        if problem is None:
            self._clear_state()
            if existed:
                _say(f"devvm: removed VM {name}")
        else:
            _say(
                f"devvm: could not remove VM {name}: {problem}. State kept; run `devvm.py destroy macos-arm64`.")

    def _cleanup(self, name: str, *, lock_held: bool) -> None:
        """Undo whatever `up` created, once. Signals are only recorded here, so it cannot be cut short."""
        if self._done or self._cleaned or not self._claimed:
            return
        self._cleaned = True
        try:
            if not self._clone_attempted:  # no VM exists: the claim must not wedge the worktree
                self._clear_state()
            elif lock_held:
                self._teardown(name)
            else:
                with self._cap_lock(interruptible=False):
                    self._teardown(name)
        except Exception as e:  # noqa: BLE001
            _say(f"devvm: cleanup of {name} failed: {e!r}. Run `devvm.py destroy macos-arm64`.")

    # up --------------------------------------------------------------------------

    def _reject_windows_flags(self, args: argparse.Namespace) -> None:
        for flag, attr in (("--allow-elevation", "allow_elevation"), ("--display", "display")):
            if getattr(args, attr, None):
                _say(f"error: {flag} only applies to Windows guests")
                sys.exit(1)

    @_reports_failures
    def up(self, args: argparse.Namespace) -> None:
        self._reject_windows_flags(args)
        try:
            sha = resolve_rev(self.repo_root, args.rev or "HEAD")
        except BadRev as e:
            _say(f"error: {e}")
            sys.exit(1)
        if BASE_IMAGE not in self._names():
            _say(
                f"error: base image {BASE_IMAGE} is not pulled. Check free disk (about 30 GB needed), then run: "
                f"{self.tart.binary} pull {BASE_IMAGE}")
            sys.exit(1)
        name = new_vm_name()
        self._proc = None
        self._claimed = self._clone_attempted = self._cleaned = self._done = False
        error = cancelled = None
        with SignalGate() as gate:
            self._gate = self.last_gate = gate
            try:
                with self._worktree_lock(interruptible=True):
                    try:
                        self._up_steps(name, sha, bool(getattr(args, "rosetta", False)))
                    finally:
                        self._cleanup(name, lock_held=False)
            except Cancelled as c:
                cancelled = c.signo
            except (AlreadyUp, UpError) as e:
                error = str(e)
            except Exception as e:  # noqa: BLE001  tart list failing, bad JSON, a full disk: a message, never a traceback
                error = _describe(e)
        self._gate = None
        # Read after the gate has restored the handlers and drained its pipe: every signal recorded at
        # any point is in `pending`, and one arriving later takes its default action.
        pending = gate.pending
        if error is not None:
            _say(f"error: {error}")
            sys.exit(128 + pending[0] if pending else 1)
        if cancelled is not None:
            _say(f"devvm: interrupted by {signal.Signals(cancelled).name}")
            sys.exit(128 + cancelled)
        _say(f"note: {name} is up; source from `git archive {sha}` is at {GUEST_TREE} (committed state only).", file=sys.stdout)
        if pending:
            _say(f"devvm: interrupted by {signal.Signals(pending[0]).name} after `up` had finished; {name} is up (`destroy` removes it)")
            sys.exit(128 + pending[0])

    def _up_steps(self, name: str, sha: str, rosetta: bool) -> None:
        cp = self._checkpoint
        cp("start")
        self._claim(name)
        self._claimed = True
        cp("claimed")
        with self._cap_lock(interruptible=True):
            try:
                cp("locked")
                self._acquire_cap_slot(name)
                cp("counted")
                self._create_vm(name)
                cp("cloned")
                self._boot(name)
                cp("booted")
            except BaseException:
                self._cleanup(name, lock_held=True)
                raise
        cp("unlocked")
        self._archive_into_guest(name, sha)
        cp("archived")
        self._provision(name, rosetta)
        cp("provisioned")
        self._done = True

    def _acquire_cap_slot(self, name: str) -> None:
        vms = self.tart.vms(gate=self._gate)
        try:
            check_cap(count_running(vms))
            require_name_free(name, vms)
        except (CapExceeded, NameTaken) as e:
            raise UpError(str(e)) from None

    def _create_vm(self, name: str) -> None:
        self._clone_attempted = True
        rc = self.tart.clone(BASE_IMAGE, name, gate=self._gate)
        if rc != 0:
            raise UpError(f"`tart clone` failed (exit {rc})")
        _write_atomic(self._identity_file, json.dumps(self.tart.identity(name)))

    def _boot(self, name: str) -> None:
        log = self.sdir / "tart-run.log"
        self._proc = self.tart.start(name, log)
        self._wait_for_agent(name, self._proc, log)

    def _wait_for_agent(self, name: str, proc: subprocess.Popen, log: Path) -> None:
        # `tart ip --wait` can return before `tart run` registers the VM, so readiness is the first exec that answers.
        # Guest boot is an external event: the wait is bounded and the failure reported; the pause between tries
        # ends at once on a signal.
        deadline = time.monotonic() + AGENT_BOOT_TIMEOUT_SECONDS
        while True:
            if self.tart.exec(name, ["true"], capture_output=True, gate=self._gate).returncode == 0:
                return
            if proc.poll() is not None:
                raise UpError(f"`tart run` for {name} exited ({proc.returncode}); see {log}")
            if time.monotonic() >= deadline:
                raise UpError(
                    f"the guest {name} did not answer within {AGENT_BOOT_TIMEOUT_SECONDS}s of starting "
                    f"(boot hang, or an image without the Tart guest agent); see {log}"
                )
            self._pause(1)

    def _provision(self, name: str, rosetta: bool) -> None:
        # Streamed from THIS checkout: an older --rev may predate the script.
        with open(self.repo_root / PROVISION_SCRIPT, "rb") as script:
            r = self.tart.exec(
                name,
                ["bash", "-c", 'bash -s -- "$HOME/cosca" "$@"', "_", *(["--rosetta"] if rosetta else [])],
                stdin=script,
                gate=self._gate,
            )
        if r.returncode != 0:
            raise UpError("provisioning failed")
        marked = self.tart.exec(
            name,
            ["sudo", "sh", "-c", f"echo 'devvm guest' > {GUEST_MARKER}"],
            gate=self._gate,
        )
        if marked.returncode != 0:
            raise UpError("could not mark the guest as a devvm guest")

    def _archive_into_guest(self, name: str, sha: str) -> None:
        git = subprocess.Popen(
            ["git", "-C", str(self.repo_root), "archive", "--format=tar", "--end-of-options", sha],
            stdout=subprocess.PIPE,
        )
        assert git.stdout is not None
        try:
            untar = self.tart.exec(
                name,
                ["sh", "-c", f"rm -rf {GUEST_TREE} && mkdir -p {GUEST_TREE} && tar -x -C {GUEST_TREE}"],
                stdin=git.stdout,
                gate=self._gate,
            )
        except BaseException:
            git.kill()
            git.wait()
            raise
        finally:
            git.stdout.close()
        if git.wait() != 0 or untar.returncode != 0:
            raise UpError(f"copying `git archive {sha}` into the guest failed")

    # other verbs -----------------------------------------------------------------

    @_reports_failures
    def sync(self, args: argparse.Namespace) -> None:
        name = self._require_vm()
        try:
            sha = resolve_rev(self.repo_root, args.rev or "HEAD")
            self._archive_into_guest(name, sha)
        except (BadRev, UpError) as e:
            _say(f"error: {e}")
            sys.exit(1)

    @_reports_failures
    def run(self, args: argparse.Namespace) -> None:
        for flag, given in (
            ("--unelevated", bool(getattr(args, "unelevated", False))),
            ("--timeout", getattr(args, "timeout", None) is not None),  # 0 is a value too
        ):
            if given:
                _say(f"error: {flag} only applies to Windows guests")
                sys.exit(1)
        cmd_args = list(args.cmd)
        if cmd_args and cmd_args[0] == "--":
            cmd_args = cmd_args[1:]
        if not cmd_args:
            _say("error: no command given; usage: devvm.py run <guest> -- <cmd...>")
            sys.exit(1)
        name = self._require_vm()
        code = self.tart.exec(name, ["bash", "-c", build_remote_command(cmd_args)]).returncode
        sys.exit(128 - code if code < 0 else code)  # a death by signal N is 128+N, as in a shell

    @_reports_failures
    def ssh(self, _args: argparse.Namespace) -> None:
        name = self._require_vm()
        self.tart.exec_interactive(name)

    @_reports_failures
    def fetch(self, args: argparse.Namespace) -> None:
        name = self._require_vm()
        try:
            guest_path = parse_guest_path(args.guest_path)
        except ValueError as e:
            _say(f"error: {e}")
            sys.exit(1)
        dest = Path(args.host_dest)
        dest.mkdir(parents=True, exist_ok=True)
        src = self.tart.exec_popen(name, ["sh", "-c", FETCH_SCRIPT, "_", guest_path], stdout=subprocess.PIPE)
        assert src.stdout is not None
        untar = subprocess.run(["tar", "-x", "-C", str(dest)], stdin=src.stdout)
        src.stdout.close()
        if src.wait() != 0 or untar.returncode != 0:
            _say(f"error: fetching {guest_path} failed")
            sys.exit(1)

    @_reports_failures
    def halt(self, _args: argparse.Namespace) -> None:
        _say("error: halt is not supported for macos-arm64 (a halted VM cannot be resumed); use `destroy`")
        sys.exit(1)

    def status(self) -> str:
        name = self._read_claim()
        if name is None:
            return "not created"
        if not name:
            return "corrupt claim"
        try:
            vms = self.tart.vms()
        except Exception as e:  # noqa: BLE001  any failure of `tart list` must not break `list`
            return f"unknown ({name}; {e})"
        for e in vms:
            if e.get("Name") == name:
                return f"{e.get('State', '?')} ({name})"
        return f"missing ({name})"

    @_reports_failures
    def destroy(self, _args: argparse.Namespace) -> None:
        """Stop and delete this worktree's VM, but only the one this worktree created.

        A signal cancels the wait for the worktree lock; once past it, destroy finishes what it started.
        """
        cancelled = failure = None
        status = 0
        with SignalGate() as gate:
            self._gate = self.last_gate = gate
            try:
                with self._worktree_lock(interruptible=True):
                    status = self._destroy_locked()
            except Cancelled as c:
                cancelled = c.signo
            except Exception as e:  # noqa: BLE001  same: a message, then the exit status
                failure = _describe(e)
        self._gate = None
        pending = gate.pending  # after the handlers are restored: nothing recorded is lost
        if failure is not None:
            _say(f"error: {failure}")
            sys.exit(128 + pending[0] if pending else 1)
        if cancelled is not None:
            _say(f"devvm: interrupted by {signal.Signals(cancelled).name} before destroy started")
            sys.exit(128 + cancelled)
        if pending:
            _say(f"devvm: interrupted by {signal.Signals(pending[0]).name}; destroy finished with status {status}")
            sys.exit(128 + pending[0])
        if status:
            sys.exit(status)

    def _destroy_locked(self) -> int:
        """Returns the exit status: 0, or 1 if destroy refused or failed."""
        name = self._read_claim()
        if name is None:
            return 0
        if not name:
            _say("warning: empty claim file; removing it (no VM is recorded)")
            self._clear_state()
            return 0
        require_own_name(name)
        if name not in self._names():
            _say(f"note: VM {name} was already gone")
            self._clear_state()
            return 0
        if not self._identity_verified(name):
            return 1
        problem = self._remove_vm(name, None)
        if problem is not None:
            _say(f"error: {problem}; leaving state in place")
            return 1
        self._clear_state()
        return 0

    def _identity_verified(self, name: str) -> bool:
        """Fail closed: a present VM is touched only if its recorded disk identity matches."""
        hint = "Inspect `tart list` and delete it by hand if it is yours."
        try:
            recorded = json.loads(self._identity_file.read_text())
        except (FileNotFoundError, ValueError):
            _say(f"error: no usable identity recorded for VM {name}, so it cannot be verified as this worktree's. {hint}")
            return False
        try:
            current = self.tart.identity(name)
        except FileNotFoundError:
            _say(f"error: VM {name} has no disk.img under {self.tart.home}/vms; cannot verify it. {hint}")
            return False
        if not identity_matches(recorded, current):
            _say(f"error: VM {name} is not the one this worktree created (its disk changed); refusing. {hint}")
            return False
        return True