Files
w3cjandClaude Opus 5.5 b6954e1e11 refactor(vmname): a VM's name is a vmname.VM, parsed where it's read
vmname.VM is the name of every machine devvm runs on a backend: a
project VM, an image's machine (devvm-img-<key>), a build VM and a
remake's aside. Fields, parameters, results and map keys that held a VM's
name as a string now hold a VM, from the backend interface down to the
CLI's --json structs, whose output is byte for byte the same.

Boundary parsers, the only places a string becomes a VM:
- vmname.Parse: a project VM's name (state's vm.json name, export file
  names, devvm _links's argument, the forwarder's -vm, project.Nth)
- vmname.ParseListed: one path element, for what a backend lists
- VM.UnmarshalText: JSON (Lima's list, proxy config and events, links'
  targets in state, link lists); empty stays no VM
- vmname.Image(key), VM.Aside(), and typed constants for Lima's build VMs
- msg.M.VM: the VM a message's function took, read back

vmname's functions on names became methods (Hash, Slug, Short, Aside,
AsideHash, ImageKey, IsProject); Lima's image prefix moved to vmname;
vmname.Join joins VM lists. Messages' vm and vm_name fields are typed
by the generator. Seven messages whose field took something else became
what= (backend_lacks, backend_unreviewed, backend_unreviewed_used,
starting, deleting, configuring, running_script) and rm_vm_failed's
name=: the text is unchanged, the names-mode scripts follow.
Review.RequireFor names an image or the tools being built.

Typed sites per package (added lines with vmname.VM, non-test): msg 347
(generated), vm 124, lima 48, backendtest 32, vmssh 31, netpolicy 22,
backend 16, gitprotect 14, proxy 11, cli 11, project 10, state 8,
ports 6, paths 3, sshagent 2, sshconfig 2, tools 2.

The structure test's owners rule now fails on a field, parameter or
result named for a VM (vm, vms, otherVM, vmName) typed string, []string
or map[string]…, and on converting anything but a literal to vmname.VM
outside the files it lists; internal/stubtest is free. depguard holds
internal/vmname to the standard library, and the proxy may import it.

ParseListed and UnmarshalText refuse a name starting with -, which would read as a flag in limactl's argv, and vmname.Image refuses a key that's empty, holds a / or NUL, or makes a project VM's name, so Lima's ImageSize and DeleteImage no longer check it themselves. The check also fails on a *string VM name and an alias of vmname.VM, and says what it doesn't see.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 19:36:28 -07:00

128 lines
4.0 KiB
Go

package vm
import (
"slices"
"strconv"
"github.com/devvmhq/devvm/internal/backend"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/netpolicy"
"github.com/devvmhq/devvm/internal/settings"
"github.com/devvmhq/devvm/internal/vmname"
)
// summary is what a new VM gets, as devvm shows it before making it: its
// parts, each with why it gets it, its size, tier and ports, and the
// devcontainer.json it's made as.
type summary struct {
vm vmname.VM
o Options
d partsDefault
p Parts
// agentsFrom and layersFrom are what chose setup's agents and layers:
// devvm setup, or config.toml.
agentsFrom, layersFrom string
size Size // CPUs, memory and disk, each set (shownSize)
tier netpolicy.Tier
learn bool // it learns the hosts it reaches, on tier internet
ports string
dc string // the devcontainer.json's path, "" for none
}
// chosenBy is what chose a default list from source: config.toml, or
// devvm setup, which saves it.
func chosenBy(source settings.Source) string {
if source == settings.FromConfig {
return string(settings.FromConfig)
}
return "devvm setup"
}
// shownSize is s with what it leaves unset as the VM gets it: base's
// CPUs and memory fitted to host (SizeChoice.Fitted), and base's disk.
func shownSize(s Size, host Host) Size {
return s.Or(SizeChoice{Host: host}.Fitted()).Or(Size{Disk: strconv.Itoa(backend.BaseDiskGiB)})
}
// lines are the summary's messages: its heading first, then a line for
// each toolchain, agent and layer, or none of one, then its size, tier,
// ports and devcontainer.json.
func (s summary) lines() []msg.M {
out := []msg.M{msg.NewVMSummary(s.vm)}
out = append(out, s.toolchains()...)
out = append(out, s.agents()...)
out = append(out, s.layers()...)
network := msg.NewVMNetwork(string(s.tier))
if s.learn {
network = msg.NewVMNetworkLearn()
}
out = append(out, msg.NewVMSize(s.size.CPUs, s.size.Disk, s.size.Memory), network, msg.NewVMPorts(s.ports))
if s.dc != "" {
out = append(out, msg.NewVMDevcontainer(s.dc))
}
return out
}
// toolchains are a line for each toolchain: from --tools, or for the file
// that calls for it.
func (s summary) toolchains() []msg.M {
if len(s.p.Toolchains) == 0 {
return []msg.M{msg.NewVMToolchainsNone()}
}
var out []msg.M
for _, t := range s.p.Toolchains {
if s.o.Toolchains != nil {
out = append(out, msg.NewVMToolchainFrom("--tools", t))
} else {
out = append(out, msg.NewVMToolchainFor(s.d.why[t], t))
}
}
return out
}
// agents are a line for each agent: from --agents, or from what chose
// setup's.
func (s summary) agents() []msg.M {
if len(s.p.Agents) == 0 {
return []msg.M{msg.NewVMAgentsNone()}
}
from := s.agentsFrom
if s.o.Agents != nil {
from = "--agents"
}
out := make([]msg.M, 0, len(s.p.Agents))
for _, a := range s.p.Agents {
out = append(out, msg.NewVMAgentFrom(a, from))
}
return out
}
// layers are a line for each layer, bottom first: from --layers, from
// what chose setup's, or for the devcontainer.json whose features call
// for it.
func (s summary) layers() []msg.M {
if len(s.p.Layers) == 0 {
return []msg.M{msg.NewVMLayersNone()}
}
out := make([]msg.M, 0, len(s.p.Layers))
for _, l := range s.p.Layers {
switch {
case s.o.Layers != nil:
out = append(out, msg.NewVMLayerFrom("--layers", l))
case slices.Contains(s.d.setupLayers, l):
out = append(out, msg.NewVMLayerFrom(s.layersFrom, l))
default:
out = append(out, msg.NewVMLayerFor(s.d.why[l], l))
}
}
return out
}
// leftToChoose reports whether customizing a new VM has anything to ask
// once o's flags are taken as given: a part of the picker's, its tier
// when tierOpen (neither --network nor a saved tier, and a backend that
// may confine it), its ports, or its CPUs, memory or disk.
func leftToChoose(o Options, tierOpen bool) bool {
return !allGiven(o) || tierOpen || o.Ports == "" || o.Size.CPUs == "" || o.Size.Memory == "" || o.Size.Disk == ""
}