Files
w3cjandClaude Opus 5.5 b625469ac6 fix(vm): devvm rm asks before opencode's worktrees' uncommitted changes go
An export leaves out opencode's worktree folder, as a git repository's config can name a command that git runs, so the uncommitted changes in the worktrees opencode makes there (~/.local/share/opencode/worktree/<project>/<name>, opencode v2.4.1's worktree/strategies.ts) went with the VM's disk on devvm rm, and on a remake with --clean-home, with nothing to say so.

The registry gets Worktrees (opencode's worktree/*/*, below its HistoryOwn, TestDefsComplete holding each to a plain pattern there). rm's unsaved-work check, cloneCheck, becomes workCheck: the clone (cloneLost, as before) and the agents' worktrees (agentTrees), one question for both. agentTreesState reads them in the running VM in one command, in its home folder, each * of a pattern sent as *, .[!.]* and ..?* (treeGlobs), so a name that starts with a dot is found: each folder with a .git, its git status and a detached HEAD's commit nothing else has; past 100 it says more. devvm reads at most 8 MiB of the answer, cancelling the command past it, and waits at most treesWait (2 minutes). parseAgentTrees takes the answer as untrusted: whole records of a glob asked for, each path clean, matched by its pattern and once, at most 100, each with one status. Dirty, detached, unreadable (wfail) and more are shown and asked about (delete_anyway or remake_anyway), which --force never answers; refused without a terminal (no_terminal_delete_trees, naming --no-worktree-check, or the clone's no_terminal_delete when it has work too). --no-worktree-check skips the check and warns (trees_check_skipped). A failed, late or hostile answer keeps the VM (trees_check_failed), and so does a VM that doesn't start for it (trees_check_not_started).

Rm and rmGone run it, and rmGone refuses a stopped VM with opencode, which starting would make its folder again (rm_gone_stopped_trees, naming --no-export --no-worktree-check); stillGone takes the refusal to give. remaking.checkWork runs it before a remake with --clean-home (remakeTo, Update, SetClone, whose clone check it joins), its messages naming the command as typed (remakeInVM.typed), with --clean-home, which --no-worktree-check needs (needs), so devvm clone off --clean-home's clone check messages name it too. A remake's flag has its own help. The stub guest runs the read in vmhome-<vm>.

Tests that fail without the check: TestRmAgentTrees, TestRmVMAgentTrees, TestRemakeAgentTrees, TestUpdateAgentTrees, TestSetCloneAgentTrees and the rm_agent_trees script; TestAgentTreesBounded (an endless answer cancelled, a silent VM given up on, in a synctest bubble); TestAgentTreesState runs the script under sh on real worktrees, a dot-named one included; TestParseAgentTrees holds the answer's checks, TestCloneGit the stub's. TestSetCloneOff names the command with --clean-home. e2e's network section makes a worktree in opencode's folder in the real VM, and devvm rm refuses, then --no-worktree-check deletes it.

Docs: coding agents' opencode's worktrees, How it works' opencode's worktrees, deleting a VM, a VM whose folder is gone, images, clone, the overview's remake, Commands, extending and rm's help; the changelog's What to do step 4 names --no-worktree-check for scripts and devvm rm VM of a stopped VM, and opencode's block gets the check; the release page has its section.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-25 00:03:05 -07:00

63 lines
2.7 KiB
Go

package cli
import (
"github.com/spf13/cobra"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/vm"
)
// newRmCmd is devvm rm [VM] [--force] [--no-export] [--no-clone-check]
// [--no-worktree-check].
func newRmCmd(a *app) *cobra.Command {
var o vm.RmOptions
cmd := &cobra.Command{
Use: "rm [VM]",
Annotations: about{
docs: "using/vms#deleting-a-vm", group: groupVM,
noJSON: "it deletes",
}.annotations(),
Short: "Delete this folder's VM, or a VM whose folder is gone",
Long: "Delete this folder's VM and what devvm keeps for it, or the VM you name when its folder is gone, after asking in a terminal without --no-input, which --force never answers when its folder is on a disk that isn't connected. " +
"When its image has an agent, devvm first exports the agents' history, unless you give --no-export. " +
"A --clone VM's work that this repo hasn't fetched is offered for fetching first, unless you give --no-clone-check, and devvm shows the uncommitted changes in the agents' worktrees and asks before they go, unless you give --no-worktree-check.",
Example: examples(
"devvm rm", "delete this folder's VM, after asking",
"devvm rm --force", "delete it without asking, as a script must",
"devvm rm --no-export", "delete it without exporting the agents' history",
"devvm rm --no-clone-check", "delete a --clone VM without checking its clone, losing its unfetched work",
"devvm rm --no-worktree-check", "delete it without checking the agents' worktrees, losing their uncommitted changes",
"devvm rm devvm-web-71a99b1076d92c", "delete a VM whose folder is gone, by its name as devvm ls lists it",
),
Args: cobra.MatchAll(maxArgs(1), noFolderFlagsWithVM),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
name := args[0]
return a.env(func(env *vm.Env) error { return vm.Lifecycle{Env: env}.RmVM(cmd.Context(), name, o) },
func(reason string) msg.M { return msg.RmVMFailed(reason, name) })
}
return a.env(func(env *vm.Env) error { return vm.Lifecycle{Env: env}.Rm(cmd.Context(), o) }, msg.RmFailed)
},
}
addForceFlag(cmd.Flags(), &o.Force)
addNoExportFlag(cmd.Flags(), &o.NoExport)
addNoCloneCheckFlag(cmd.Flags(), &o.NoCloneCheck)
addNoWorktreeCheckFlag(cmd.Flags(), &o.NoWorktreeCheck)
addFolderFlags(cmd, a)
return cmd
}
// noFolderFlagsWithVM refuses --here and --parent with a VM's name: they
// choose this folder's VM, and devvm rm VM deletes the one it names.
func noFolderFlagsWithVM(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return nil
}
for _, name := range []string{"here", "parent"} {
if cmd.Flags().Changed(name) {
return msg.RmVMFolderFlag("--" + name)
}
}
return nil
}