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

145 lines
4.7 KiB
Go

package backend_test
import (
"errors"
"io/fs"
"testing"
"github.com/devvmhq/devvm/internal/backend"
"github.com/devvmhq/devvm/internal/backend/backendtest"
"github.com/devvmhq/devvm/internal/msg"
)
// stored is a backend whose disks are in dirs, with half a GiB for each
// tool; nothing else of it is called.
func stored(dirs ...string) backendtest.Stored {
f := backendtest.DefaultFloors()
f.PerTool = 512 << 20
return backendtest.Stored{Dirs: dirs, Floor: f}
}
// freeOf answers each folder's free space from a table, and errs for the
// rest.
func freeOf(free map[string]uint64, err error) func(string) (uint64, error) {
return func(dir string) (uint64, error) {
if n, ok := free[dir]; ok {
return n, nil
}
return 0, err
}
}
func imageOf(f backend.Floors) uint64 { return f.Image }
func TestSpaceCheck(t *testing.T) {
const need = 8 << 30
statErr := &fs.PathError{Op: "statfs", Path: "/gone", Err: fs.ErrPermission}
b := stored("/lima")
for _, tc := range []struct {
name string
b backend.Backend
need func(backend.Floors) uint64
free func(string) (uint64, error)
want string // the message's names, "" for none
}{
{"below", b, imageOf, freeOf(map[string]uint64{"/lima": need - 1}, nil), "disk_low dir=/lima free=7.9 name=node need=8"},
{"at", b, imageOf, freeOf(map[string]uint64{"/lima": need}, nil), ""},
{"above", b, imageOf, freeOf(map[string]uint64{"/lima": 100 << 30}, nil), ""},
{
"the lowest volume",
stored("/a", "/b"),
imageOf,
freeOf(map[string]uint64{"/a": 50 << 30, "/b": 1 << 30}, nil),
"disk_low dir=/b free=1.0 name=node need=8",
},
{
"the backend's own floor", b, func(f backend.Floors) uint64 { return f.Base },
freeOf(map[string]uint64{"/lima": need}, nil),
"disk_low dir=/lima free=8.0 name=node need=12",
},
{
// 3 GiB and 0.5 more for each of three tools, 4.5, is rounded up.
"rounded up to whole GiB", b, func(f backend.Floors) uint64 { return f.ForTools(1, 3) },
freeOf(map[string]uint64{"/lima": 9 << 29}, nil),
"disk_low dir=/lima free=4.5 name=node need=5",
},
{"statfs fails", b, imageOf, freeOf(nil, statErr), "disk_free_unknown dir=/lima"},
{"a system without it", b, imageOf, freeOf(nil, errors.ErrUnsupported), ""},
{"no free space func", b, imageOf, nil, ""},
{"a backend whose disks aren't the host's", stored(), imageOf, freeOf(nil, statErr), ""},
} {
t.Run(tc.name, func(t *testing.T) {
err := backend.Space{Free: tc.free}.Check(tc.b, "node", tc.need)
if got := msg.NamesOf(err); got != tc.want {
t.Errorf("Check = %q, want %q", got, tc.want)
}
if tc.name == "statfs fails" && !errors.Is(err, statErr) {
t.Errorf("statfs's error isn't passed on: %v", err)
}
})
}
}
func TestSpaceCheckDir(t *testing.T) {
free := freeOf(map[string]uint64{"/data": 1<<30 - 1}, nil)
if got := msg.NamesOf(backend.Space{Free: free}.CheckDir("/data", "node", backend.ToolImages(1))); got != "disk_low dir=/data free=0.9 name=node need=1" {
t.Errorf("CheckDir = %q", got)
}
enough := freeOf(map[string]uint64{"/data": 1 << 30}, nil)
if err := (backend.Space{Free: enough}).CheckDir("/data", "node", backend.ToolImages(1)); err != nil {
t.Errorf("CheckDir with enough = %v", err)
}
}
func TestForTools(t *testing.T) {
f := backend.Floors{Tools: 3 << 30, PerTool: 512 << 20}
if got := f.ForTools(1, 10); got != 8<<30 {
t.Errorf("ForTools(1, 10) = %d", got)
}
// Four build VMs at once, each one a copy of base where the disk has
// no copy on write.
if got := f.ForTools(4, 10); got != 17<<30 {
t.Errorf("ForTools(4, 10) = %d", got)
}
if got := f.ForTools(0, 10); got != 8<<30 {
t.Errorf("ForTools(0, 10) = %d, want one build VM's", got)
}
if got := backend.ToolImages(10); got != 3<<30 {
t.Errorf("ToolImages(10) = %d", got)
}
}
func TestSpaceWarning(t *testing.T) {
b := stored("/lima")
for _, tc := range []struct {
name string
b backend.Backend
free func(string) (uint64, error)
want string
}{
{"below base's", b, freeOf(map[string]uint64{"/lima": 11 << 30}, nil), "disk_low_warning dir=/lima free=11.0 need=12"},
{"at base's", b, freeOf(map[string]uint64{"/lima": 12 << 30}, nil), ""},
{"unreadable", b, freeOf(nil, fs.ErrPermission), ""},
{"a backend whose disks aren't the host's", stored(), freeOf(nil, nil), ""},
} {
t.Run(tc.name, func(t *testing.T) {
m, low := backend.Space{Free: tc.free}.Warning(tc.b)
got := ""
if low {
got = m.Names()
}
if got != tc.want {
t.Errorf("Warning = %q, want %q", got, tc.want)
}
})
}
}
func TestGiB(t *testing.T) {
for n, want := range map[uint64]string{0: "0.0", 1 << 30: "1.0", 12<<30 - 1: "11.9", 3<<29 + 1: "1.5"} {
if got := backend.GiB(n); got != want {
t.Errorf("GiB(%d) = %s, want %s", n, got, want)
}
}
}