Files
devvm/tests/e2e/opencode_worktrees_test.go
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

57 lines
2.3 KiB
Go

//go:build e2e
package e2e
import "errors"
// opencodeWorktree is where the check makes a worktree as opencode 2.4
// makes its own: worktree/<the project's ID's first 6 characters>/<name>
// in its data folder, with git worktree add.
const opencodeWorktree = "~/.local/share/opencode/worktree/e2e123/brave"
// netOpencodeWorktrees: devvm rm of vm, an opencode VM in the folder oc,
// finds a worktree in opencode's folder with an uncommitted change, which
// an export leaves out, shows it and, without a terminal, refuses, naming
// --no-worktree-check, and keeps the VM; --no-worktree-check warns and
// deletes it.
func netOpencodeWorktrees(n *netRun, oc, vm string) {
s := n.s
if vm == "" {
return
}
env := n.home("DEVVM_MESSAGES=names")
s.Check("devvm rm shows an opencode worktree's uncommitted change, and without a terminal keeps the VM", func() error {
plant := "git init -q ~/e2e-repo && git -C ~/e2e-repo -c user.name=e -c user.email=e@e commit -q --allow-empty -m one && " +
"git -C ~/e2e-repo worktree add -q -b brave " + opencodeWorktree + " && echo x > " + opencodeWorktree + "/new"
if err := inVM(s, vm, plant); err != nil {
return failed("making a worktree in opencode's folder", err)
}
r, err := s.Run(devvmCmd(s, oc, "", env, "rm", "--force", "--no-export"))
switch {
case err == nil:
return errors.New("devvm rm deleted the VM")
case !Matches(r.Out, `^agent_tree_dirty agent=opencode path=~/\.local/share/opencode/worktree/e2e123/brave$`) || !HasLine(r.Out, "?? new"):
return errors.New("devvm rm didn't show the worktree's change")
case !Matches(r.Out, `^devvm: no_terminal_delete_trees command=devvm rm$`):
return errors.New("devvm rm didn't refuse, naming --no-worktree-check")
}
if l, _ := s.Limactl("list", "-q"); !l.Has(vm) {
return errors.New("the VM is gone")
}
return nil
})
s.Check("... and devvm rm --no-worktree-check deletes it after a warning", func() error {
r, err := s.Run(devvmCmd(s, oc, "", env, "rm", "--force", "--no-export", "--no-worktree-check"))
switch {
case err != nil:
return failed("devvm rm --no-worktree-check", err)
case !Matches(r.Out, `^devvm: trees_check_skipped command=devvm rm vm=`+vm+`$`):
return errors.New("devvm rm --no-worktree-check didn't warn")
}
if l, _ := s.Limactl("list", "-q"); l.Has(vm) {
return errors.New("the VM is still there")
}
return nil
})
}