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

422 lines
16 KiB
Go

package agents
import (
"cmp"
"os"
"path"
"path/filepath"
"regexp"
"slices"
"strings"
"testing"
"github.com/BurntSushi/toml"
)
// root is the repo's root, from this package's folder.
const root = "../.."
// Every agent has what the rest of devvm finds it by, beside its Def: its
// shared tool's backend in shared-tools/mise.toml's [tool_alias], so
// mise's registry can't move it, and its row in the docs' Supported
// agents table, in the registry's order. (Its policy and its [tools] line
// are every shared tool's, which internal/image's TestSharedToolsPolicies
// and internal/tools' TestConfigTools check, and its login files' formats
// internal/agentlogin's TestFormatsCoverLogins.)
func TestDefsComplete(t *testing.T) {
var mise struct {
Alias map[string]string `toml:"tool_alias"`
}
if _, err := toml.DecodeFile(filepath.Join(root, "assets", "shared-tools", "mise.toml"), &mise); err != nil {
t.Fatal(err)
}
var names []string
for _, row := range pageTable(t, "## Supported agents") {
names = append(names, row[0])
}
if !slices.Equal(names, Names()) {
t.Errorf("Supported agents lists %v, the registry %v, in its order", names, Names())
}
for _, d := range Defs {
if mise.Alias[d.Name] == "" {
t.Errorf("%s: shared-tools/mise.toml's [tool_alias] has no line for it", d.Name)
}
}
}
// pageTable is the rows of the table in coding-agents.md's section of
// that heading whose first cell is a name in backticks, each row's cells
// trimmed, the first without its backticks.
func pageTable(t *testing.T, heading string) [][]string {
t.Helper()
page, err := os.ReadFile(filepath.Join(root, "docs", "content", "docs", "using", "coding-agents.md"))
if err != nil {
t.Fatal(err)
}
_, section, ok := strings.Cut(string(page), "\n"+heading+"\n")
if !ok {
t.Fatalf("coding-agents.md has no %q section", heading)
}
level, _, _ := strings.Cut(heading, " ")
section, _, _ = strings.Cut(section, "\n"+level+" ")
var rows [][]string
for _, line := range strings.Split(section, "\n") {
if !strings.HasPrefix(line, "| `") {
continue
}
cells := strings.Split(strings.Trim(line, "|"), "|")
for i := range cells {
cells[i] = strings.TrimSpace(cells[i])
}
cells[0] = strings.Trim(cells[0], "`")
rows = append(rows, cells)
}
return rows
}
// The providers are each whole and sorted by ID, and the docs' table of
// them names each, in that order, with its name, its hosts and its header.
func TestProvidersComplete(t *testing.T) {
code := regexp.MustCompile("`([^`]+)`")
spans := func(s string) []string {
var out []string
for _, m := range code.FindAllStringSubmatch(s, -1) {
out = append(out, m[1])
}
return out
}
rows := pageTable(t, "### API keys the proxy puts in")
if len(rows) != len(Providers) {
t.Errorf("the docs' table has %d providers, the registry %d", len(rows), len(Providers))
}
for i, p := range Providers {
if p.ID == "" || p.Display == "" || len(p.Hosts) == 0 || len(p.Headers) == 0 || slices.Contains(p.Headers, "") {
t.Errorf("%+v: no ID, name, hosts or headers", p)
}
// The proxy reads a bindings file whose path is anything else as none (proxy.ReadBindings).
if p.Path != "" && (!strings.HasPrefix(p.Path, "/") || !strings.HasSuffix(p.Path, "/") || path.Clean(p.Path)+"/" != p.Path) {
t.Errorf("%s: path %q doesn't start and end with / in its clean form", p.ID, p.Path)
}
var paths []string
if p.Path != "" {
paths = []string{p.Path}
}
if i > 0 && Providers[i-1].ID >= p.ID {
t.Errorf("%s: after %s, out of order", p.ID, Providers[i-1].ID)
}
if got, ok := ProviderOf(p.ID); !ok || got.ID != p.ID {
t.Errorf("ProviderOf(%s) = %v, %v", p.ID, got, ok)
}
if i >= len(rows) {
continue
}
row := rows[i]
if len(row) != 5 || row[0] != p.ID || row[1] != p.Display || !slices.Equal(spans(row[2]), p.Hosts) ||
!slices.Equal(spans(row[3]), paths) || !slices.Equal(spans(row[4]), p.Headers) {
t.Errorf("the docs' table's row %v isn't %+v", row, p)
}
}
if ids := ProviderIDs(); len(ids) != len(Providers) || !slices.IsSorted(ids) {
t.Errorf("ProviderIDs = %v", ids)
}
if _, ok := ProviderOf("none"); ok {
t.Error("ProviderOf finds a provider that isn't one")
}
}
// Each Def is whole and its own: a name, its folder, logins of kinds no
// other has, the first of its name, history excludes inside its history,
// and settings inside its folder.
func TestDefsWellFormed(t *testing.T) {
kinds := map[string]bool{}
for _, d := range Defs {
if d.Name == "" || d.Display == "" || d.Folder == "" {
t.Errorf("%+v: no name, display name or folder", d)
}
if d.Docs.Adds == "" || d.Docs.Reaches == "" || strings.Contains(d.Docs.Adds+d.Docs.Reaches, "|") {
t.Errorf("%s: its row in the docs' table has no Adds or Reaches, or one breaks the table", d.Name)
}
if len(d.Logins) > 0 && d.Logins[0].Kind != d.Name {
t.Errorf("%s: its first login's kind is %s", d.Name, d.Logins[0].Kind)
}
for _, l := range d.Logins {
if kinds[l.Kind] {
t.Errorf("%s: login kind %s is another's too", d.Name, l.Kind)
}
kinds[l.Kind] = true
if got, a, ok := LoginOf(l.Kind); !ok || got != l || a.Name != d.Name {
t.Errorf("LoginOf(%s) = %v, %s, %v", l.Kind, got, a.Name, ok)
}
}
for _, x := range slices.Concat(d.HistoryExcludes, d.HistoryOwn) {
if !slices.ContainsFunc(d.History, func(h string) bool { return x == h || strings.HasPrefix(x, h+"/") }) {
t.Errorf("%s: history exclude %s is outside its history %v", d.Name, x, d.History)
}
}
// A worktree's folder is one an export leaves out, which is why it's checked, and its
// pattern goes to the VM's shell unquoted, so it's only of a path's plain characters and *.
for _, p := range d.Worktrees {
if !slices.ContainsFunc(d.HistoryOwn, func(o string) bool { return strings.HasPrefix(p, o+"/") }) ||
strings.Trim(p, "abcdefghijklmnopqrstuvwxyz0123456789./-_*") != "" || path.Clean(p) != p {
t.Errorf("%s: worktrees %q aren't a plain pattern below its HistoryOwn", d.Name, p)
}
}
for _, p := range slices.Concat(d.Dirs, filesOf(d)) {
if !strings.HasPrefix(p, d.Folder+"/") {
t.Errorf("%s: setting %s is outside its folder %s", d.Name, p, d.Folder)
}
}
// A pattern goes into the save's script as a case pattern and into the setup's
// machine-own file as a glob, so it's only of a name's plain characters and *.
for _, p := range d.FolderOwn {
if _, err := path.Match(p, ""); err != nil || !d.WholeFolder || strings.Trim(p, "abcdefghijklmnopqrstuvwxyz0123456789.-_*") != "" {
t.Errorf("%s: FolderOwn %q isn't a plain pattern of a WholeFolder agent's entry", d.Name, p)
}
}
for _, s := range d.Seeds {
if !slices.Contains(d.Dirs, s.Rel) || !strings.HasPrefix(s.Link, d.Folder+"/") || slices.Contains(d.Dirs, s.Link) || len(s.Entries) == 0 {
t.Errorf("%s: seed %+v isn't one of its folders, or its link isn't a path of its own", d.Name, s)
}
}
if got, ok := Get(d.Name); !ok || got.Name != d.Name {
t.Errorf("Get(%s) = %v", d.Name, ok)
}
}
if _, ok := Get("none"); ok {
t.Error("Get finds an agent that isn't one")
}
if _, _, ok := LoginOf("none"); ok {
t.Error("LoginOf finds a login that isn't one")
}
}
func filesOf(d Def) []string {
var out []string
for _, f := range d.Files {
out = append(out, f.Rel)
}
return out
}
// The settings files whose VM copies devvm parses, and those it withholds.
func TestSettingsFormat(t *testing.T) {
for rel, want := range map[string]Format{
".claude/settings.json": JSON, ".codex/config.toml": TOML, OpencodeConfig + "/opencode.json": JSON,
OpencodeConfig + "/tui.jsonc": JSON, OpencodeConfig + "/x.toml": TOML, PiSettings: JSON,
".claude/CLAUDE.md": AsIs, PiFolder + "/keybindings.json": AsIs, ".claude/other.json": AsIs,
} {
if got, ok := SettingsFormat(rel); got != want || ok != (want != AsIs) {
t.Errorf("SettingsFormat(%s) = %q, %v; want %q", rel, got, ok, want)
}
}
for rel, want := range map[string]bool{
OpencodeConfig + "/.env": true, OpencodeConfig + "/notes.txt": true, OpencodeConfig + "/AGENTS.md": false,
OpencodeConfig + "/opencode.json": false, OpencodeConfig + "/plugin/x.env": false, ".codex/x.env": false,
} {
if got := Withheld(rel); got != want {
t.Errorf("Withheld(%s) = %v, want %v", rel, got, want)
}
}
}
// The lists of every agent's are each Def's, in Defs' order, and Of,
// which code of one agent's own asks with its constant, finds it.
func TestLists(t *testing.T) {
var history, excludes, logins, kinds []string
var env []Env
for _, d := range Defs {
history, env = append(history, d.History...), append(env, d.Env...)
excludes = append(append(excludes, d.HistoryExcludes...), d.HistoryOwn...)
logins = append(logins, d.HistoryExcludes...)
for _, l := range d.Logins {
kinds = append(kinds, l.Kind)
}
}
if got := History(); !slices.Equal(got, history) || !slices.Contains(got, ClaudeJSON) {
t.Errorf("History = %v", got)
}
if got := HistoryExcludes(); !slices.Equal(got, excludes) || !slices.Contains(got, OpencodeState+"/machine") {
t.Errorf("HistoryExcludes = %v", got)
}
if got := HistoryLogins(); !slices.Equal(got, logins) || slices.Contains(got, OpencodeState+"/machine") {
t.Errorf("HistoryLogins = %v", got)
}
if got := GuestEnv(); !slices.Equal(got, env) || !slices.Contains(got, Env{Name: "PI_TELEMETRY", Value: "0"}) {
t.Errorf("GuestEnv = %v", got)
}
if got := LoginKinds(); !slices.Equal(got, kinds) || !slices.Contains(got, ClaudeOAuth) {
t.Errorf("LoginKinds = %v", got)
}
if Of(Pi).Folder != PiFolder {
t.Error("Of(pi)")
}
defer func() {
if recover() == nil {
t.Error("Of of no agent didn't panic")
}
}()
Of("none")
}
// Claude Code's plugins folder is a seed: its marketplaces, their records and the plugins'
// cache are what a seed has (Claude Code's docs, plugins/org.md), and every other entry of the
// folder is each machine's own, such as each plugin's data (plugins/loading.md); nothing outside
// the folder is.
func TestSeeds(t *testing.T) {
s, ok := SeedOf(ClaudePlugins)
if all := Seeds(); !ok || s.Link != ClaudePluginSeed || len(all) != 1 || all[0].Rel != ClaudePlugins {
t.Errorf("SeedOf(%s) = %+v, %v; Seeds = %+v", ClaudePlugins, s, ok, Seeds())
}
if _, ok := SeedOf(".claude/skills"); ok {
t.Error("skills is a seed")
}
for rel, want := range map[string]bool{
".claude/plugins/data": true, ".claude/plugins/data/p/state.json": true, ".claude/plugins/mcpb/b": true,
".claude/plugins/synced/s": true, ".claude/plugins/.trash/t": true, ".claude/plugins/store/s.json": true,
".claude/plugins/installed_plugins.json": true, ".claude/plugins/known_marketplaces_claudeai.json": true,
".claude/plugins/flagged-plugins.json": true,
".claude/plugins": false, ".claude/plugins/cache/m/p/1.0.0/x": false, ".claude/plugins/marketplaces/m": false,
".claude/plugins/known_marketplaces.json": false, ".claude/plugins-seed/data": false, ".claude/skills/data": false,
} {
if got := MachineOwn(rel); got != want {
t.Errorf("MachineOwn(%s) = %v, want %v", rel, got, want)
}
}
// opencode's service settings (opencode v2.4.1's service-config.ts): service.json on its
// default channels, service-<channel>.json on the others, their temporary files, and
// whatever is below an entry of that name; none of its other files, nor a file of that name
// below a subfolder or another agent's folder.
for rel, want := range map[string]bool{
OpencodeConfig + "/service.json": true, OpencodeConfig + "/service-local.json": true,
OpencodeConfig + "/service-0123abcd.json": true, OpencodeConfig + "/service.json.4242.tmp": true,
OpencodeConfig + "/service-local.json.4242.tmp": true, OpencodeConfig + "/service.json/x": true,
OpencodeConfig + "/opencode.json": false, OpencodeConfig + "/services.json": false, OpencodeConfig + "/service.jsonc": false,
OpencodeConfig + "/agent/service.json": false, OpencodeConfig: false, ".claude/service.json": false,
} {
if got := MachineOwn(rel); got != want {
t.Errorf("MachineOwn(%s) = %v, want %v", rel, got, want)
}
}
env := GuestEnv()
for _, v := range []Env{
{Name: "CLAUDE_CODE_PLUGIN_CACHE_DIR", Value: ClaudePlugins, Home: true},
{Name: "CLAUDE_CODE_PLUGIN_SEED_DIR", Value: ClaudePluginSeed, Home: true},
} {
if !slices.Contains(env, v) {
t.Errorf("GuestEnv lacks %+v", v)
}
}
}
// The command lines devvm agent login runs, exactly: Claude Code's
// subscription login and claude setup-token without the VM's
// CLAUDE_CODE_OAUTH_TOKEN, which Claude Code would use instead; Codex's
// ChatGPT login kept in its file, never a keyring; and opencode's Codex
// logins to OpenAI, in a browser or with a code. pi has none.
func TestLoginRun(t *testing.T) {
const store = `cli_auth_credentials_store="file"`
want := map[string][3][]string{ // its login, with a code, with a setup token
Claude: {
{"env", "-u", "CLAUDE_CODE_OAUTH_TOKEN", "claude", "auth", "login", "--claudeai"},
{"env", "-u", "CLAUDE_CODE_OAUTH_TOKEN", "claude", "auth", "login", "--claudeai"},
{"env", "-u", "CLAUDE_CODE_OAUTH_TOKEN", "claude", "setup-token"},
},
Codex: {
{"codex", "-c", store, "login"},
{"codex", "-c", store, "login", "--device-auth"},
{"codex", "-c", store, "login"},
},
Opencode: {
{"opencode", "auth", "login", "openai", "--method", "chatgpt-browser"},
{"opencode", "auth", "login", "openai", "--method", "chatgpt-headless"},
{"opencode", "auth", "login", "openai", "--method", "chatgpt-browser"},
},
Pi: {},
// Its login prints the address rather than opening a browser in the VM.
Cursor: {
{"env", "NO_OPEN_BROWSER=1", "cursor-agent", "login"},
{"env", "NO_OPEN_BROWSER=1", "cursor-agent", "login"},
{"env", "NO_OPEN_BROWSER=1", "cursor-agent", "login"},
},
}
for _, d := range Defs {
w, ok := want[d.Name]
if !ok {
t.Errorf("%s: no command lines in TestLoginRun", d.Name)
continue
}
for i, flags := range [][2]bool{{false, false}, {true, false}, {false, true}} {
if got := d.Login.Run(flags[0], flags[1]); !slices.Equal(got, w[i]) {
t.Errorf("%s: Run(%v, %v) = %q, want %q", d.Name, flags[0], flags[1], got, w[i])
}
}
}
}
// Each agent's own login runs the agent, a forwarded one a ChatGPT login
// that has a login with a code too, and devvm agent login runs the command
// line its flags choose, or the login's own when the agent has no such
// one: a copy, which the caller can't change the Def by.
func TestLoginCmds(t *testing.T) {
runs := func(name string, argv []string) bool {
return len(argv) > 0 && (argv[0] == name || argv[0] == "env" && slices.Contains(argv, name))
}
or := func(a, b []string) []string {
if a != nil {
return a
}
return b
}
for _, d := range Defs {
l := d.Login
if l.Argv == nil {
if l.Device != nil || l.SetupToken != nil || l.Forward || l.Paste {
t.Errorf("%s: no login of its own, but %+v", d.Name, l)
}
continue
}
for _, argv := range [][]string{l.Argv, l.Device, l.SetupToken} {
if command := cmp.Or(d.Command, d.Name); argv != nil && !runs(command, argv) {
t.Errorf("%s: %q doesn't run %s", d.Name, argv, command)
}
}
if l.Forward && (!d.ChatGPT || l.Device == nil) {
t.Errorf("%s: a login forwarded without a ChatGPT login or a login with a code", d.Name)
}
if l.SetupToken != nil && d.Secret == "" {
t.Errorf("%s: a setup token, but no secret to save it as", d.Name)
}
for _, tc := range []struct {
device, token bool
want []string
}{
{false, false, l.Argv},
{true, false, or(l.Device, l.Argv)},
{false, true, or(l.SetupToken, l.Argv)},
} {
got := l.Run(tc.device, tc.token)
if !slices.Equal(got, tc.want) {
t.Errorf("%s: Run(%v, %v) = %q, want %q", d.Name, tc.device, tc.token, got, tc.want)
}
got[0] = "changed"
if tc.want[0] == "changed" {
t.Errorf("%s: Run returned the Def's own command line", d.Name)
}
}
}
if !slices.ContainsFunc(Defs, func(d Def) bool { return d.Login.Device != nil }) ||
!slices.ContainsFunc(Defs, func(d Def) bool { return d.Login.SetupToken != nil }) {
t.Error("no agent has a login with a code or a setup token, so Run's choices go untested")
}
}
// The agents devvm agent login logs in, and those it saves keys alone for.
func TestLoginAgents(t *testing.T) {
if got := LoginAgents(); !slices.Equal(got, Names()) {
t.Errorf("LoginAgents = %v", got)
}
if got := Select(Def.KeysOnly); !slices.Contains(got, Pi) || slices.Contains(got, Claude) || slices.Contains(got, Codex) {
t.Errorf("KeysOnly agents = %v", got)
}
}