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

250 lines
7.6 KiB
Go

package msg
import (
"cmp"
"context"
"io"
"os/exec"
"slices"
"sync"
)
// A Reporter is what devvm's packages need of the run's UI: where their
// messages go, the questions they ask and the picker, the status
// line, the run log, output that isn't a message, and the run's cleanup
// stack. internal/ui's UI is the one devvm runs with, which internal/cli
// gives each package that embeds or takes a Reporter; tests give them a
// Fake.
type Reporter interface {
// Info says what devvm did and Say prints a line, both on stdout; Note
// prints a line and Warn a warning, both on stderr.
Info(M)
Say(M)
Note(M)
Warn(M)
// Status shows a status line until stop.
Status(label M) (stop func())
// Ask asks a yes-or-no question, no by default ([y/N]); Confirm one
// that's yes by default ([Y/n]); Answer asks for a value, "" for
// Enter. Each returns the question's *Refusal instead when it can't
// ask: with --no-input, without a terminal, or at the end of the
// input.
Ask(Question) (bool, error)
Confirm(Question) (bool, error)
Answer(Question) (string, error)
// CanPick reports whether the picker is shown, which keeps its
// choices as they are when it isn't.
CanPick() bool
// Logo prints devvm's logo, once a run, where someone watches stdout.
Logo()
// LogNote says where the run log is, once a run; LogWriter is the run
// log, for lines that aren't messages; LoggedCmd runs a command into
// it; WithSteps numbers the logged commands of a phase of so many
// steps.
LogNote()
LogWriter() io.Writer
LoggedCmd(ctx context.Context, step M, cmd *exec.Cmd) error
WithSteps(ctx context.Context, total int) context.Context
// Stdout is where data output goes, Stderr output on stderr that
// isn't a message.
Stdout() io.Writer
Stderr() io.Writer
// AddCleanup adds to the run's cleanup stack; Interrupted reports
// whether the run was interrupted.
AddCleanup(f func(context.Context)) (remove func())
Interrupted() bool
}
var _ Reporter = (*Fake)(nil)
type logTagKey struct{}
// WithLogTag returns ctx for logged commands that run beside others, such
// as tool images building side by side: the run log names each line of
// theirs with tag ("[node] "), so their lines can be told apart, and they
// show no status line of their own, as one for their group covers them.
func WithLogTag(ctx context.Context, tag string) context.Context {
return context.WithValue(ctx, logTagKey{}, tag)
}
// LogTagOf is ctx's tag (WithLogTag), "" for none.
func LogTagOf(ctx context.Context) string {
s, _ := ctx.Value(logTagKey{}).(string)
return s
}
// A Fake is a Reporter for tests. Its zero value is quiet: it prints
// nothing, has no terminal, so it refuses every question, drops the run
// log and data output, runs a logged command as it is, its output
// dropped, counts no steps, keeps no cleanups and isn't interrupted. Its
// fields change that: each On func, when set, is given what its method
// is (a Recorder's Add records a kind of message), and the rest stand
// for the terminal, the writers and an interrupt. A test may change them
// after giving it to what it tests. It keeps the status lines started
// and not stopped (Showing), so it's never copied once used.
type Fake struct {
OnInfo, OnSay, OnNote, OnWarn func(M)
// OnAny is given every message, whatever its kind, after its kind's
// On func.
OnAny func(M)
// OnAsk, OnConfirm and OnAnswer are given the question asked, each
// after its context goes to OnNote: no and "" without them.
OnAsk, OnConfirm func(M) bool
OnAnswer func(M) string
// OnCleanup is AddCleanup.
OnCleanup func(f func(context.Context)) (remove func())
// OnStatus is given each status line's label as it starts, and each
// logged command's step as it runs (the zero M for none); Showing is
// the one shown meanwhile, so a test can check that slow work ran
// under its line.
OnStatus func(M)
// Terminal is whether questions are asked: without it each is
// refused. Picks is CanPick.
Terminal, Picks bool
// Interrupt is Interrupted.
Interrupt bool
// Out is Stdout, Err Stderr and Log LogWriter: nil drops what's
// written.
Out, Err, Log io.Writer
status struct {
sync.Mutex
lines []*M // the status lines started and not stopped, innermost last
}
}
// Info gives m to OnInfo and OnAny.
func (f *Fake) Info(m M) { f.say(f.OnInfo, m) }
// Say gives m to OnSay and OnAny.
func (f *Fake) Say(m M) { f.say(f.OnSay, m) }
// Note gives m to OnNote and OnAny.
func (f *Fake) Note(m M) { f.say(f.OnNote, m) }
// Warn gives m to OnWarn and OnAny.
func (f *Fake) Warn(m M) { f.say(f.OnWarn, m) }
// Status gives label to OnStatus and keeps it shown (Showing) until stop.
func (f *Fake) Status(label M) (stop func()) {
if f.OnStatus != nil {
f.OnStatus(label)
}
line := &label
f.status.Lock()
f.status.lines = append(f.status.lines, line)
f.status.Unlock()
var once sync.Once
return func() {
once.Do(func() {
f.status.Lock()
defer f.status.Unlock()
if i := slices.Index(f.status.lines, line); i >= 0 {
f.status.lines = slices.Delete(f.status.lines, i, i+1)
}
})
}
}
// Showing is the label of the innermost status line or logged command's
// step showing now, and whether one is: what the UI's status line would
// show.
func (f *Fake) Showing() (M, bool) {
f.status.Lock()
defer f.status.Unlock()
if len(f.status.lines) == 0 {
return M{}, false
}
return *f.status.lines[len(f.status.lines)-1], true
}
// Ask is OnAsk's answer, no without one; without Terminal, q's refusal.
func (f *Fake) Ask(q Question) (bool, error) {
if err := f.ask(q); err != nil {
return false, err
}
return f.OnAsk != nil && f.OnAsk(q.M()), nil
}
// Confirm is OnConfirm's answer, no without one; without Terminal, q's
// refusal.
func (f *Fake) Confirm(q Question) (bool, error) {
if err := f.ask(q); err != nil {
return false, err
}
return f.OnConfirm != nil && f.OnConfirm(q.M()), nil
}
// Answer is OnAnswer's answer, "" without one; without Terminal, q's
// refusal.
func (f *Fake) Answer(q Question) (string, error) {
if err := f.ask(q); err != nil || f.OnAnswer == nil {
return "", err
}
return f.OnAnswer(q.M()), nil
}
// ask is q's refusal without Terminal; with it, q's context said.
func (f *Fake) ask(q Question) error {
if !f.Terminal {
return q.Refused(false)
}
for _, m := range q.Context() {
f.Note(m)
}
return nil
}
// CanPick is Picks.
func (f *Fake) CanPick() bool { return f.Picks }
// Logo prints nothing.
func (f *Fake) Logo() {}
// LogNote says nothing.
func (f *Fake) LogNote() {}
// LogWriter is Log, or nowhere.
func (f *Fake) LogWriter() io.Writer { return cmp.Or(f.Log, io.Discard) }
// LoggedCmd runs cmd as it is, its step shown meanwhile (Status) unless
// it runs beside others (WithLogTag), as the UI's does.
func (f *Fake) LoggedCmd(ctx context.Context, step M, cmd *exec.Cmd) error {
if LogTagOf(ctx) == "" {
defer f.Status(step)()
}
return cmd.Run()
}
// WithSteps returns ctx.
func (f *Fake) WithSteps(ctx context.Context, _ int) context.Context { return ctx }
// Stdout is Out, or nowhere.
func (f *Fake) Stdout() io.Writer { return cmp.Or(f.Out, io.Discard) }
// Stderr is Err, or nowhere.
func (f *Fake) Stderr() io.Writer { return cmp.Or(f.Err, io.Discard) }
// AddCleanup gives c to OnCleanup; without one it keeps nothing.
func (f *Fake) AddCleanup(c func(context.Context)) func() {
if f.OnCleanup == nil {
return func() {}
}
return f.OnCleanup(c)
}
// Interrupted is Interrupt.
func (f *Fake) Interrupted() bool { return f.Interrupt }
// say gives m to on, then OnAny, each when it's set.
func (f *Fake) say(on func(M), m M) {
for _, g := range []func(M){on, f.OnAny} {
if g != nil {
g(m)
}
}
}