Files
w3cjandClaude Opus 5.5 dd46dd7a0f feat(tools): tool images build side by side
A first run built its tool images one after another, each paying a
build VM's boot (5.7 s on vz, 9.3 s on QEMU) before a download of a few
seconds to a minute. Share.Build now hands the images out to up to four
slots (buildAll), each slot's builds one after another in a build VM of
its own:

- slots: as many as the host's memory, less what the backend's running
  VMs have, and its CPUs give a build VM of 2 GiB and 2 CPUs each after
  keeping 6 GiB and 2 CPUs, at most backend.MaxToolSlots (4) and the
  number of images; one alone keeps base's size. A backend that can't
  list its VMs builds one at a time. A running VM's memory counts whole
  (7.5 GiB, not 7): TestBuildSlotsFit's running VMs fail when it isn't
  counted or is truncated.
- plan lowers the slots until the backend's disks have room:
  Floors.ForTools counts Tools once for each build VM at once, as on a
  disk without copy on write each is a whole copy of base.
- backend.ToolsSpec gets Slot, Slots, Size and Hold; Lima's slot VMs are
  the constants ToolsVM to ToolsVM4, sized with --cpus/--memory, and the
  first slot's build deletes what a killed run left in the slots this
  run doesn't use. Builds hold the share's lock, so one devvm uses the
  slots.
- A failed build's VM goes in the run's cleanup, which runs after the
  build let go of the share's lock: it deletes the VM only once it has
  the lock again (ToolsSpec.Hold, Env.holdShare's TryLockFile), and
  leaves it while another devvm holds it (a build, gc or the daily
  refresh) (TestBuildToolsCleanupHolds, TestBuildHoldsShareForCleanup).
  The next tools build deletes what it finds in the slots, and so does
  devvm gc, which holds the share's lock: backend.Images'
  DeleteToolsLeft, Lima's deleting the slots' build VMs that are there
  and no other VM, which gc asks of every backend first, without
  asking, a backend that can't being a run log line. It returns what
  it deleted, which gc names in one line (tools_build_vms_deleted), and
  gc then never says nothing_to_clean or nothing_deleted
  (gc_tools_build.txtar, TestDeleteToolsLeft in lima and vm, the fake's
  ToolsLeft). The cli skill's judgment.md names this exception to "gc
  --force deletes exactly what gc lists". The conformance suite's
  delete-tools-left checks that it deletes no other VM, not even the
  project VM of a folder named tools (devvm-tools-<hash>), and no image
  (TestConformanceFinds: deletesAllVMs, deletesImage, deletesByPrefix).
  The share's lock
  is the data folder's, so devvms with their own data folders and one
  Lima home don't exclude each other's builds: the docs say not to build
  tools or run gc in two at once (a lock per build VM in the Lima home
  would close it, but it would replace Hold and needs e2e).
- Side by side, one status line covers the builds
  (building_tools_together), and their commands' lines in the run log
  are tagged with their tool (msg.WithLogTag): the UI's LoggedCmd
  prefixes them and shows no status line of its own. Every write starts
  with the tag, as does every line in it, one ended by "\r" included,
  so no build can pass a line off as another's (TestTaggedWrites), and
  a tool's lines are found after another's "\r" (taggedLines splits
  there, a progress bar's updates staying one line: TestTaggedLines). The
  first failure cancels the others and is what Build returns, tagged
  (backend.LoggedIn), so ErrorLogged shows that tool's lines' end.
- A test build's DEVVM_TOOL_SLOTS caps the slots; the CLI tests set 1,
  so the stub's calls keep their order, and tools_side_by_side.txtar
  sets 3, with an interrupt that leaves no build VM. The stub keeps each
  build VM's folder by its name, and the fake backend's BuildTools runs
  its builds outside its lock, as a real backend's would, passing
  ToolsBuild its context.

Docs: images.md's Building tools and Free space, ports-size.md,
space.md, images.md (using) and gc's help (the build VMs gc deletes),
settings.md (one Lima home for two data folders), extending.md,
testing.md and tests.md; the cli skill's judgment.md; the CHANGELOG's
first highlight and the release page's A faster first run.

New messages: building_tools_together, tools_build_vms_deleted.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 22:57:15 -07:00

397 lines
13 KiB
Go

// Package tools keeps the shared tools: the toolchains and agents image
// names hold (node, claude), which aren't image layers. Each tool version
// is one read-only image in the share, built once in a throwaway VM and
// mounted into every VM that uses it, where mise runs it.
//
// Which versions is per user: devvm's shared-tools/mise.toml says which
// releases (node 24, the latest claude), and the host's mise resolves it
// into the tools lock,
// with each download's checksum per platform. Builds install exactly the
// lock's versions, checked against those checksums. The lock is resolved
// on first use and again when mise.toml changes; once a day a
// launch resolves it again in the background, so VMs can move to newer
// versions. A VM lists its tool images in its state, so a newer version
// never changes a VM that has one.
//
// Env is what the shared tools run with; Versions keeps the lock (resolving
// it, the daily check, the image files an image name's tools are); Share
// builds, lists and deletes the share's tool images; VMs moves a VM to
// newer tools.
package tools
import (
"bytes"
"context"
"errors"
"io/fs"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/devvmhq/devvm/internal/backend"
"github.com/devvmhq/devvm/internal/daemon"
"github.com/devvmhq/devvm/internal/fetch"
"github.com/devvmhq/devvm/internal/image"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/paths"
"github.com/devvmhq/devvm/internal/platform"
"github.com/devvmhq/devvm/internal/settings"
"github.com/devvmhq/devvm/internal/state"
)
// ShareAt is where VMs have the share, read-only, and where the tools
// build has its output folder.
const ShareAt = "/mnt/devvm-tools"
// checkEvery is how often the lock is resolved again.
const checkEvery = 24 * time.Hour
// Env is what one user's shared tools run with, shared by Versions, Share
// and VMs; its methods are what they share: the platforms, devvm's
// mise.toml, a tool's image file and the share's lock. New sets every
// field; tests change them.
type Env struct {
Dirs paths.Dirs
// Assets holds devvm's own files (assets.FS): shared-tools/mise.toml
// and shared-tools/build.sh.
Assets fs.FS
// Self is devvm's binary, which the daily check runs; Version its
// version, which each tool image's record keeps.
Self, Version string
// OS and Arch are the host's, as Go names them.
OS, Arch string
Getenv func(string) string
// Client downloads the host's mise, through Proxy (its Route).
Client *fetch.Client
// Proxy is the company proxy the host's mise is downloaded through
// and runs with, and that a tool build checks before it starts; its
// caller sets its Password (secrets.Store.ProxyPassword).
Proxy *settings.CompanyProxy
// RetryDelay is the wait before a download's first retry; it doubles
// after each.
RetryDelay time.Duration
Now func() time.Time
// Detach starts the daily check, which outlives the run
// (daemon.Detach).
Detach daemon.Starter
// Reporter is where it prints, the status line and run log its slow
// steps show on, and the run's cleanup stack. Env's own Warn says
// where the run log is too.
msg.Reporter
// FreeSpace returns the free space on the volume holding a path
// (platform.Host's), checked before a build; nil checks none.
FreeSpace func(path string) (uint64, error)
// Memory and CPUs are the host's (platform.Host's), which cap how many
// tool images build side by side (slots); nil builds one at a time.
Memory func() (uint64, error)
CPUs func() int
// MaxSlots is the most that build side by side: a test build's
// stand-in (internal/cli's Overrides); 0 for backend.MaxToolSlots.
MaxSlots int
// Mise is a test build's stand-in for devvm's pinned mise on the
// host (internal/cli's Overrides); "" in a release.
Mise string
// The host's mise: its download's URL (a format taking the version
// and platform) and checksums, by platform.
miseURL string
miseSums map[string]string
// dotnetURL is a .NET channel's releases.json (a format taking the
// channel), which the lock's dotnet downloads come from.
dotnetURL string
}
// Versions is the tools lock: resolving it with the host's mise, the daily
// check, and the image files of an image name's tools at its versions.
type Versions struct{ *Env }
// Share is the share's tool images: building what it lacks, listing and
// deleting them.
type Share struct{ *Env }
// VMs is a VM's shared tools against the lock: whether newer ones are
// there, and moving the VM to them.
type VMs struct{ *Env }
// Warn prints a warning, and where the run log has more.
func (t *Env) Warn(m msg.M) {
t.Reporter.Warn(m)
t.LogNote()
}
// New returns the shared tools in devvm's folders, with devvm's own files
// (assets), for devvm's version, on r, checking host's free space before
// a build.
func New(d paths.Dirs, assets fs.FS, version string, r msg.Reporter, host platform.Host) *Env {
self, err := os.Executable()
if err == nil {
if r, rerr := filepath.EvalSymlinks(self); rerr == nil {
self = r
}
}
t := &Env{
Dirs: d, Assets: assets, Self: self, Version: version, OS: runtime.GOOS, Arch: runtime.GOARCH,
Getenv: os.Getenv, Client: newClient(), RetryDelay: time.Second, Now: time.Now, Detach: daemon.Detach,
miseURL: miseURL, miseSums: miseSHA256, dotnetURL: dotnetReleases,
Reporter: r, FreeSpace: host.FreeSpace, Memory: host.Memory, CPUs: host.CPUs,
}
// The environment it reads is Getenv's, and the Proxy the one at each
// request: tests and callers change them.
t.Proxy = settings.NewCompanyProxy(d, func(k string) string { return t.Getenv(k) }, nil)
if tr := t.Client.Transport(); tr != nil {
tr.Proxy = func(req *http.Request) (*url.URL, error) { return t.Proxy.Route(req) }
}
return t
}
// MiseArch is mise's name for a Go architecture: the host's, and so its
// VMs'.
func MiseArch(goarch string) string {
if goarch == "amd64" {
return "x64"
}
return "arm64"
}
// platforms are the platforms the lock resolves: the VMs', glibc and musl.
func (t *Env) platforms() []string {
a := MiseArch(t.Arch)
return []string{"linux-" + a, "linux-" + a + "-musl"}
}
// platform is the platform of a VM whose base has libc.
func (t *Env) platform(libc string) string {
p := "linux-" + MiseArch(t.Arch)
if libc == "musl" {
p += "-musl"
}
return p
}
// configFile is devvm's mise.toml of the shared tools, in its assets.
const configFile = "shared-tools/mise.toml"
// config is devvm's shared-tools/mise.toml.
func (t *Env) config() ([]byte, error) { return fs.ReadFile(t.Assets, configFile) }
// Aliases is the [tool_alias] table of devvm's shared-tools/mise.toml,
// each tool's backend, for a VM's mise config: the VM's mise then takes
// each tool from where its image's came from, whatever its registry says.
func Aliases(assets fs.FS) ([]byte, error) {
data, err := fs.ReadFile(assets, configFile)
if err != nil {
return nil, err
}
aliases, err := configAliases(data)
if err != nil {
return nil, err
}
return aliasTable(aliases), nil
}
// Lock returns the tools lock, resolving it first when there's none yet,
// it isn't TOML, or shared-tools/mise.toml changed since (a devvm
// upgrade).
func (t Versions) Lock(ctx context.Context) (Lock, error) {
want, err := t.config()
if err != nil {
return Lock{}, err
}
data, lerr := os.ReadFile(t.Dirs.ToolLock())
from, ferr := os.ReadFile(t.Dirs.ToolLockConfig())
if lerr == nil && ferr == nil && bytes.Equal(from, want) {
if lock, err := ParseLock(data); err == nil {
return lock, nil
}
}
t.Info(msg.ResolvingTools())
lock, err := t.Resolve(ctx, t.Dirs.ToolLock())
if err != nil {
// A certificate mise doesn't trust is said as it is: being online
// again doesn't help.
if _, ok := errors.AsType[*backend.LoggedError](err); ok && !errors.Is(err, errUntrusted) {
return Lock{}, backend.Logged(msg.ToolsNeedResolve())
}
return Lock{}, err
}
return lock, t.touchChecked()
}
func (t Versions) touchChecked() error {
now := t.Now()
err := os.Chtimes(t.Dirs.ToolLockChecked(), now, now)
if errors.Is(err, fs.ErrNotExist) {
err = state.WriteFile(t.Dirs.ToolLockChecked(), nil)
if err == nil {
err = os.Chtimes(t.Dirs.ToolLockChecked(), now, now)
}
}
return err
}
// CheckDue reports whether the daily check is due: there's a lock, not
// resolved in the last day. Never with DEVVM_NO_TOOL_CHECK set.
func (t Versions) CheckDue() bool {
if t.Getenv("DEVVM_NO_TOOL_CHECK") != "" {
return false
}
if _, err := os.Stat(t.Dirs.ToolLock()); err != nil {
return false
}
fi, err := os.Stat(t.Dirs.ToolLockChecked())
return err != nil || t.Now().Sub(fi.ModTime()) >= checkEvery
}
// RefreshDaily starts devvm _refresh-tools in the background once a day,
// when the check is due, so a later launch knows of newer versions. It
// doesn't wait for it.
func (t Versions) RefreshDaily() error {
if !t.CheckDue() {
return nil
}
if err := t.touchChecked(); err != nil {
return err
}
if err := state.PrivateDir(t.Dirs.Logs()); err != nil {
return err
}
out, err := os.OpenFile(t.Dirs.ToolsCheckLog(), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600) //nolint:forbidigo // the check's log, which it writes as it runs: each check's starts empty
if err != nil {
return err
}
defer func() { _ = out.Close() }()
_, err = t.Detach(daemon.Cmd{Path: t.Self, Args: []string{"_refresh-tools"}, Out: out})
return err
}
// Refresh resolves the lock again, and swaps the new one in while no
// build is reading it. A failure leaves the lock as it was.
func (t Versions) Refresh(ctx context.Context) error {
next := t.Dirs.ToolLock() + ".next"
if _, err := t.Resolve(ctx, next); err != nil {
return err
}
unlock, err := t.LockShare(ctx)
if err != nil {
return err
}
defer unlock()
if err := os.Rename(next+".toml", t.Dirs.ToolLockConfig()); err != nil {
return err
}
if err := os.Rename(next, t.Dirs.ToolLock()); err != nil {
return err
}
return t.touchChecked()
}
// Check is Refresh for a user waiting on it: a failure is a warning, and
// the versions it has stay.
func (t Versions) Check(ctx context.Context) {
t.Info(msg.CheckingTools())
if err := t.Refresh(ctx); err != nil {
if ctx.Err() == nil {
t.Warn(msg.ToolsCheckFailed())
}
}
}
// Target is what a VM's tool images are built for: its base's C library
// and its backend's image format, with the glibc version a --from gnu base
// has, which each tool image's needs are checked against (Runs).
type Target struct {
Libc string // gnu or musl
Format string // the backend's ToolFormat: "" for shared-tools/build.sh's own
// Glibc is a --from gnu base's glibc version, as its record has it;
// "" when it has none (Ubuntu's own base, musl), which isn't checked.
// A gnu tool image is the same whatever glibc its base has, so its
// key doesn't hash it.
Glibc string
// Base is, for musl, the digest of the --from base the images build
// in (its record's), which their keys hash: musl images are never
// shared beyond VMs on that base. gnu images always build in devvm's
// own Ubuntu base, so have none.
Base string
}
// File returns a tool's image file at the lock's version, for a VM of
// that target.
func (t *Env) File(lock Lock, tool string, tg Target) (string, error) {
v := lock.Version(tool)
if v == "" {
return "", msg.ToolNotLocked(t.Dirs.ToolLock(), tool)
}
entry, err := lock.Entry(tool)
if err != nil {
return "", err
}
return image.ToolFile(t.Assets, image.Tool{Name: tool, Version: v, LockEntry: entry, Libc: tg.Libc, Base: tg.Base, Format: tg.Format})
}
// Files returns the image files of the tools behind each of these names
// (node is node, pnpm and bun), at the lock's versions.
func (t Versions) Files(ctx context.Context, names []string, tg Target) ([]string, error) {
if len(names) == 0 {
return nil, nil
}
lock, err := t.Lock(ctx)
if err != nil {
return nil, err
}
var files []string
for _, n := range names {
for _, tool := range image.GroupTools(n) {
f, err := t.File(lock, tool, tg)
if err != nil {
return nil, err
}
files = append(files, f)
}
}
return files, nil
}
// noDownload reports whether the lock has no download of a tool for a VM
// whose base has libc (no version had one), or builds it from source,
// which mise falls back to.
func (t Share) noDownload(lock Lock, tool, libc string) bool {
return !lock.HasDownload(tool, []string{t.platform(libc)})
}
// split returns a VM's tool files as tool and version, keeping the order.
func split(files []string) (tools, versions []string) {
for _, f := range files {
tool, v, _ := image.ParseToolFile(f)
tools, versions = append(tools, tool), append(versions, v)
}
return tools, versions
}
// Label lists tool files as "node 24.21.0, bun 1.4.2".
func Label(files []string) string {
var out []string
for _, f := range files {
tool, v, _ := image.ParseToolFile(f)
out = append(out, tool+" "+v)
}
return strings.Join(out, ", ")
}
// Mounts is a VM's tool list as the VM reads it: a "FILE TOOL VERSION"
// line per tool image.
func Mounts(files []string) []byte {
var b bytes.Buffer
tools, versions := split(files)
for i, f := range files {
if tools[i] == "" {
continue
}
b.WriteString(f + " " + tools[i] + " " + versions[i] + "\n")
}
return b.Bytes()
}