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

216 lines
6.5 KiB
Go

package cli
import (
"context"
"crypto/x509"
"errors"
"os"
"time"
"github.com/devvmhq/devvm/assets"
"github.com/devvmhq/devvm/internal/backend"
"github.com/devvmhq/devvm/internal/backend/registry"
"github.com/devvmhq/devvm/internal/build"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/paths"
"github.com/devvmhq/devvm/internal/platform"
"github.com/devvmhq/devvm/internal/secrets"
"github.com/devvmhq/devvm/internal/settings"
"github.com/devvmhq/devvm/internal/state"
"github.com/devvmhq/devvm/internal/tools"
"github.com/devvmhq/devvm/internal/trust"
"github.com/devvmhq/devvm/internal/ui"
"github.com/devvmhq/devvm/internal/vm"
)
// app is one run's services, on its UI: devvm's folders, a test build's
// overrides, the secrets store, the backends' registry, the shared
// tools, the environment of the commands on a folder's VM and the image
// builder of the backend new VMs get. Each is made the first time a
// command asks for it, failing or not, and that one is kept for the rest
// of the run. NewRoot makes one and hands it to every command.
type app struct {
u *ui.UI
// folder is the command's --here and --parent, which vmEnv's
// environment finds the folder's VM by.
folder folderFlags
folders made[paths.Dirs]
overrides made[Overrides]
store made[*secrets.Store]
reg made[*backend.Registry]
tl made[*tools.Env]
vm made[*vm.Env]
b made[*build.Env]
}
// newApp returns the services of a run on u, none of them made yet.
func newApp(u *ui.UI) *app { return &app{u: u} }
// made is one of a run's services: the value and error of its first get,
// kept for every get after it. n counts the times it was made, never more
// than once, which the tests check.
type made[T any] struct {
v T
err error
n int
}
// get returns the service, making it with mk the first time.
func (m *made[T]) get(mk func() (T, error)) (T, error) {
if m.n == 0 {
m.n++
m.v, m.err = mk()
}
return m.v, m.err
}
// dirs is devvm's folders, as the environment gives them.
func (a *app) dirs() (paths.Dirs, error) { return a.folders.get(paths.FromEnv) }
// testOverrides is this build's overrides, read from the environment
// (readOverrides): a release's are empty. A test build's that can't be
// read panics: the test that set it is wrong, and a release never gets
// here with one.
func (a *app) testOverrides() Overrides {
o, err := a.overrides.get(func() (Overrides, error) { return readOverrides(os.Getenv) })
if err != nil {
panic("devvm: a test build's override: " + err.Error())
}
return o
}
// host is the host devvm runs on, as this build reads it: a test build's
// with its overrides.
func (a *app) host() platform.Host { return platform.NewHost(a.testOverrides().Host) }
// secrets is devvm's secrets store, warning through the run's UI.
func (a *app) secrets() (*secrets.Store, error) {
return a.store.get(func() (*secrets.Store, error) {
d, err := a.dirs()
if err != nil {
return nil, err
}
return secrets.NewStore(d, a.u), nil
})
}
// registry is the registry of devvm's backends, with this build's
// overrides, their own requests through the company proxy, its password
// from devvm's secrets.
func (a *app) registry() (*backend.Registry, error) {
return a.reg.get(func() (*backend.Registry, error) {
d, err := a.dirs()
if err != nil {
return nil, err
}
store, err := a.secrets()
if err != nil {
return nil, err
}
cp := settings.NewCompanyProxy(d, os.Getenv, store.ProxyPassword)
roots := func() (*x509.CertPool, error) { return trust.Pool(d.TrustedCAs()) }
return registry.New(d, assets.FS, a.u, a.testOverrides().Overrides, cp, roots)
})
}
// tools is the shared tools in devvm's folders, checking the host's free
// space before a build, with this build's mise, through the company
// proxy, its password from devvm's secrets.
func (a *app) tools() (*tools.Env, error) {
return a.tl.get(func() (*tools.Env, error) {
d, err := a.dirs()
if err != nil {
return nil, err
}
store, err := a.secrets()
if err != nil {
return nil, err
}
t := tools.New(d, assets.FS, devvmVersion(), a.u, a.host())
t.Mise, t.MaxSlots = a.testOverrides().HostMise, a.testOverrides().ToolSlots
t.Proxy.Password = store.ProxyPassword
return t, nil
})
}
// vmEnv is the environment the commands on this folder's VM run in, on
// the run's registry, shared tools and secrets, asking in its terminal,
// finding the VM by the command's --here and --parent.
func (a *app) vmEnv() (*vm.Env, error) {
return a.vm.get(func() (*vm.Env, error) {
d, err := a.dirs()
if err != nil {
return nil, err
}
self, err := paths.Self()
if err != nil {
return nil, err
}
reg, err := a.registry()
if err != nil {
return nil, err
}
tl, err := a.tools()
if err != nil {
return nil, err
}
store, err := a.secrets()
if err != nil {
return nil, err
}
e := vm.NewEnv(d, assets.FS, self, devvmVersion(), reg, tl, store, a.u)
e.PickFeatures = pickFeatures(a.u)
e.PickParts = pickParts(a.u)
e.PickOne = pickOne(a.u)
e.PickMany = pickMany(a.u)
e.Terminal = func() (*os.File, *os.File, func()) { return ui.Terminal(a.u) }
e.Folder = a.folder.rule()
e.HistoryMax = a.testOverrides().HistoryMax
e.PasteWait = time.Duration(a.testOverrides().PasteWait) * time.Second
if v := a.testOverrides().Volume; v != "" {
e.VolumesOnly = true
e.Volumes = func(context.Context, platform.FolderStat) (string, error) {
if v == "fail" {
return "", errors.New("DEVVM_VOLUME=fail")
}
return v, nil
}
}
e.Proxy.SSHList = a.testOverrides().SSHList()
if r := a.testOverrides().Range(); r != (platform.PortRange{}) {
e.Proxy.PortRange = r
}
clip := platform.Clipboard{Tools: a.testOverrides().ClipTools}
e.ReadClip, e.CopyText = clip.ReadClip, clip.CopyText
return e, nil
})
}
// builder is what builds images on the backend new VMs get (the VM
// environment's Builder), its base devvm's own until its caller sets
// From.
func (a *app) builder() (*build.Env, error) {
return a.b.get(func() (*build.Env, error) {
env, err := a.vmEnv()
if err != nil {
return nil, err
}
c, err := env.Registry.For(new(state.VM))
if err != nil {
return nil, err
}
return env.Builder(c.Backend, ""), nil
})
}
// env runs do in this folder's environment and returns its failure
// (failed), with what's message for an error that isn't one.
func (a *app) env(do func(*vm.Env) error, what func(reason string) msg.M) error {
env, err := a.vmEnv()
if err == nil {
err = do(env)
}
return failed(err, what)
}