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

187 lines
5.7 KiB
Go

package tools
import (
"context"
"os"
"slices"
"strings"
"github.com/devvmhq/devvm/internal/image"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/state"
"github.com/devvmhq/devvm/internal/vmname"
)
// Section is the name of the shared tools section in a VM's state.
const Section = "tools"
// InVM is a VM's shared tools section: its shared tools by name, as
// asked, in their order (image.SortTools), and the tool images devvm gave
// it.
type InVM struct {
Names []string `json:"names,omitempty"`
Made Made `json:"made,omitzero"`
}
// Made is the tool images devvm gave a VM: their files in the share, and
// the C library its base had, which they're built for, with the glibc
// version of a --from gnu base (Target's Glibc).
type Made struct {
Files []string `json:"files,omitempty"`
Libc string `json:"libc,omitempty"`
Glibc string `json:"glibc,omitempty"`
}
// Of returns a VM's shared tools section.
func Of(st *state.VM) (InVM, error) { return state.Section[InVM](st, Section) }
// Set sets a VM's shared tools section, in st alone: inside st.Update.
func Set(st *state.VM, t InVM) error { return state.SetSection(st, Section, t) }
// Names returns a VM's shared tools by name: node claude. A section that
// can't be read (the folder's VM's is checked as it's found) has none.
func Names(st *state.VM) []string {
t, _ := Of(st)
return t.Names
}
// Files returns a VM's tool images, as Names does.
func Files(st *state.VM) []string {
t, _ := Of(st)
return t.Made.Files
}
// VMTool is one of a VM's shared tools, as devvm tools lists it.
type VMTool struct {
Tool string // its name in the lock: node, pnpm
Version string // the VM's version of it
// Newer is the lock's version when the VM has another build, else "";
// Built is whether the share has that one.
Newer string
Built bool
}
// Show returns a VM's tools, from its tool files, in their order. The
// lock is read as it is, never resolved: without one, none is newer.
func (t VMs) Show(files []string, tg Target) ([]VMTool, error) {
lock, err := Versions(t).ReadLock()
if err != nil {
return nil, err
}
share := Share(t)
var out []VMTool
for _, f := range files {
tool, v, ok := image.ParseToolFile(f)
if !ok {
continue
}
vt := VMTool{Tool: tool, Version: v}
// Another file is another build: a newer version, or the same
// version from a changed lock entry or build script, as WarnNewer
// tells it.
if lv := lock.Version(tool); lv != "" {
if cur, err := t.File(lock, tool, tg); err == nil && cur != f {
vt.Newer, vt.Built = lv, share.Built(cur)
}
}
out = append(out, vt)
}
return out, nil
}
// Remove takes shared tools out of a VM with these shared tools (names)
// and tool files, made from img: it returns the VM's shared tools and
// tool files without them. What the VM installed itself stays. A layer
// ran its script into the VM's disk, so it can't be removed, and neither
// can a shared tool the VM lacks.
func Remove(vm vmname.VM, img image.Name, names, files, gone []string) ([]string, []string, error) {
var drop []string
for _, n := range gone {
switch {
case img.Has(n):
return nil, nil, msg.CustomLayerRemove(n, vm)
case !slices.Contains(names, n):
return nil, nil, msg.VMLacks(listOrNone(names), n, vm)
}
drop = append(drop, image.GroupTools(n)...)
}
var rest []string
for _, h := range names {
if !slices.Contains(gone, h) {
rest = append(rest, h)
}
}
var kept []string
tools, _ := split(files)
for i, f := range files {
if !slices.Contains(drop, tools[i]) {
kept = append(kept, f)
}
}
return rest, kept, nil
}
// listOrNone is names as a message shows a list of them: space-separated,
// or none.
func listOrNone(names []string) string {
if len(names) == 0 {
return "none"
}
return strings.Join(names, " ")
}
// Newer returns the shared tools names at the lock's versions that a VM
// with these tool files doesn't have, as "claude 2.1.295".
func (t VMs) Newer(ctx context.Context, names, have []string, tg Target) ([]string, error) {
files, err := Versions(t).Files(ctx, names, tg)
if err != nil {
return nil, err
}
var newer []string
for _, f := range files {
if !slices.Contains(have, f) {
newer = append(newer, Label([]string{f}))
}
}
return newer, nil
}
// WarnNewer warns when there are newer shared tools than a VM's: moving
// to them is cheap, as nothing in the VM's disk changes. It never changes
// the VM.
func (t VMs) WarnNewer(ctx context.Context, names, have []string, tg Target) error {
newer, err := t.Newer(ctx, names, have, tg)
if err != nil {
return err
}
if len(newer) > 0 {
t.Warn(msg.NewerTools(strings.Join(newer, ", ")))
}
return nil
}
// Update moves a VM to the newest of its shared tools (names): it
// checks upstream first (unless there's no lock yet: resolving it is
// checking), has build build what the share lacks of them (the VM's own,
// when they're the newest and one is gone from the share), and returns the
// VM's new tool files. changed is false when they were the newest already.
func (t VMs) Update(ctx context.Context, build func(context.Context, []string) error, vm vmname.VM, names, have []string, tg Target) (files []string, changed bool, err error) {
versions := Versions(t)
if _, err := os.Stat(t.Dirs.ToolLock()); err == nil {
versions.Check(ctx)
}
if files, err = versions.Files(ctx, names, tg); err != nil {
return nil, false, err
}
if err := build(ctx, files); err != nil {
return nil, false, err
}
// The same files in any order: tools added since are listed last.
if slices.Equal(slices.Sorted(slices.Values(files)), slices.Sorted(slices.Values(have))) {
t.Info(msg.ToolsNewest(vm))
return files, false, nil
}
t.Info(msg.ToolsSwitch(vm))
return files, true, nil
}