Backends, toolchains and agents
How a backend plugs in
devvm runs every VM on Lima, but the rest of devvm reaches Lima only through the backend.Backend interface in internal/backend, so another backend is an addition rather than a change to the callers. A backend lives in a package of its own under internal/backend, and it does four things:
It implements
backend.Backendand returns its errors instead of ending the run: amsg.M, orbackend.Logged(m)once a logged command failed, so the command shows the run log’s end before the message.Backendis the backend’sNameandCapsand four roles, each an interface of its own that every backend implements in full:backend.Hostis what the backend needs of this machine and where it keeps its VMs there.Problemslists what the host lacks for it, andbackend.Check(ctx, b)returns the first of them, so a backend doesn’t write a check of its own.NameMaxis the longest VM name it takes,Locationis where its VMs live when that can change between runs (backend.LocationOf), andDiskDirsandFloorsare the folders its disks are in and the free space it wants there (backend.Space). A backend whose VMs can’t move returns""fromLocation, and one whose disks aren’t on this machine returns noDiskDirs.backend.Imagesbuilds images and the shared tools’ images and keeps the images.BuildImageruns the spec’sRecipe(image.Recipe, whichinternal/buildputs together andimage.Inputhashes) as it is, eachimage.Stepwith its arguments and as root when it says so.Originis the backend’s own steps that base starts with, such as the Ubuntu image Lima pinned on this machine, a--fromimage’s conversion, base’s disk size (backend.BaseDiskGiB) andbase.yaml. Base’s size isbackend.BaseCPUs,backend.BaseMemoryGiBandbackend.BaseDiskGiB, never read from a backend’s files.internal/buildputs them first in base’s recipe, as itsOrigin, so base’s key hashes them, andBuildImageis given them back and runs those rather than reading its own files again.PinOriginpins what base starts from on this machine, whichinternal/buildasks for before it keys a build (backend.PinFirst): on Lima, that’s the newest Ubuntu cloud image (state.UbuntuPin, found byubuntu.Finder). A base build’sImageSpec.Packages, abackend.Packages, sends its packages into the VM’sbackend.PackagesDirbefore the steps and takes them out after, whichinternal/build/packages.gorecords or replays and checks, and a step that exitsbackend.ExitUnreachablefails asbuild_sources_unreachable.DeleteToolsLeftdeletes the throwaway VMs thatBuildToolsleft, such as a killed run’s, and never another VM or an image, and it returns the ones it deleted, which devvm gc names. devvm gc asks for it while it holds the share’s lock, so no build of this data folder’s is using them, but a devvm with another data folder and the same Lima home holds a lock of its own (DEVVM_LIMA_HOME), so the two mustn’t build tools or run gc at once. A backend whose builds leave none does nothing.backend.Machinesmakes, starts, stops, renames and deletes VMs, lists them, and runs commands and shells in them. A stopped VM changes only atStart, which first checks withbackend.Validatethat it can apply the VM’sbackend.Want, such as a disk no smaller than the VM’s own, and then applies what theWantasks where it differs from the VM.Startleaves a running VM as it is, and the callers, not the backend, warn that a change waits for the VM’s next start. On Lima,Startowns the config’senv:,propagateProxyEnv,dnsandhostResolver, and a project VM’s size, agent mounts andforwardAgent, which it compares with the VM’s before its one edit (what devvm sets on a VM). Ports are never the backend’s:vmssh.Forwardsforwards them over the backend’sSSH.Runningsays how a VM runs now, in onebackend.Run: the ID of its current start, whether it’s confined to its proxy, and its lifeline, the file that the VM’s proxy and port forwarder follow. The backend runs no port forwarder of its own, andSSHsays how ssh, links and devvm’s forwarder (vmssh.Forwards) reach a running VM’s sshd, a target thatvmssh.FromTargetchecks. Every ssh of devvm’s to a VM, its port forwarder’s included, refuses a target that isn’t on this machine’s loopback,127.0.0.1, withvmssh.FromTarget(what each backend emulates). All three get it throughvmssh.Checked, which checks the review lets the backend’s ssh through, checks the target, and adds the VM’s host keys for its current start, reading the record (vmssh.ReadKeys) or pinning the keys first (vmssh.PinKeys). A VM whose ssh isn’t usable fails with a*vmssh.UnusableErrorand one without host keys withvmssh.ErrNoHostKeys, which the ssh config skips on.Execpasses itsbackend.ExecOptsstreams on as they are. When they’re a terminal’s files, the command must get a terminal in the VM, as a shell does, because an agent’s login that draws full-screen needs one, and any other streams are pipes (TestExecStreamsAsGivenon Lima).backend.Housekeepingis what devvm space and devvm gc ask:Usage,Strays, andDevvmCacheandDeleteCachefor its own caches. A backend that can’t tell returns none. devvm gc also asksbackend.ImagesforDeleteToolsLeft, which sits besideBuildTools, whose VMs it deletes.
backend.Forgetteris the one role a backend may leave out, for the files it keeps for a VM besides the VM. Lima keeps none. Lima’s type,lima.Lima, holds the backend’s fields,Name,Capsand the helpers its roles share, and it embeds one type for each role,host,images,machinesandhousekeeping, each over theLimait’s part of.backendtest.Fakeis split the same way. So no type has more methods than the structure test allows, and a new method goes on the type of its role.It declares
Caps, saying for each ofbackend.Featureswhether it has the feature natively, emulates it, or lacks it (features). It lists every feature by hand,Noneincluded, andRegistry.RegisterrefusesCapsthat leave one out or rate a feature better than the oneFeature.BuiltOnsays it’s built on (Caps.Check). The callers, not the backend, decide what a VM needs, and they check it with the registry’sReview.Require(b, vm, spec.Features()...)before they act. A feature the backend lacks fails in one line that names the VM and what to do, and devvm never falls back to something weaker. An emulation stays refused until its review passes: a design note in the design notes, a threat check with the security-audit skill, and end-to-end checks that prove its guarantee.It’s registered in
registry.New(internal/backend/registry), the only place besidesinternal/clithat imports a backend. The registry hands the backend out behind its guard, which checks everyCreateVM,Start,BuildImageandBuildToolscall before the backend sees it. The guard refuses a spec that needs a featureReview.Requirewould refuse, using the specs’ ownFeatures, and a build spec whoseImageSpec.ValidateorToolsSpec.Validatefails, such as one with a network that isn’t a build’s or a key that isn’t one. So a caller that forgetsRequirestill can’t reach a backend with a feature it lacks, and the backend doesn’t check those specs itself. The guard has every role’s methods, its own in place of the backend’s for those calls, so a guarded backend handed on as abackend.Imagesor abackend.Machinesis still guarded. The guard’s type has onlybackend.Backend’s methods, so code that looks for an optional role, such asbackend.Forgetter, usesbackend.Asinstead of a type assertion, and the structure test fails on a type assertion to a backend package’s type anywhere but the guard.It names its images by key, and an image’s key hashes the backend’s name, so two backends never share an image.
It passes the conformance suite,
backendtest.Conformance(t, factory), which checks the contract thatbackend.Backend’s documentation gives. The backend’s tests call it with a factory that returns a newbackendtest.Subjectfor each check, and each check runs as a subtest. The subject is the backend, holding one image, whose key isImage, and no VMs.Diris a folder of the check’s own for the folders its VMs mount,Bootssays whether its VMs start, andRestartstarts a running VM again outsideStart, as a command of the user’s own would. The suite checks every role’s answers and the empty answers each role allows, that a VM is made, renamed and deleted as its spec says, and that a missing VM isbackend.ErrNotFound. It also checks thatRunninggives a stopped VM its lifeline with an emptyID, that every start gets anotherID, that a VM isConfinedonly while it runs on a startStartmade withNetProxy, and that the registry’s guard still refuses what the backend lacks when the backend is used asbackend.Imagesorbackend.Machinesalone.checkProxyURLchecks what the open tier needs of every backend, which has no feature of its own (features): a VM on the backend’s own network (NetDirect) reaches a port on the host’s loopback, and the guest’s proxy settings come from the confinement (Confinement.GuestProxy,ProxyURL). WhenBootsis false, the checks that need a running VM are skipped with the reason, and whenRestartis nil, so is the one that needs it.backendtest.Fakeruns the suite ininternal/backend/backendtest, andTestConformanceininternal/backend/limaruns it on Lima twice: on the limactl stub, whose VMs start, and on real Lima’s dummy instances, which can’t boot, so the checks that need a running VM are left to the end-to-end tests. The real Lima run skips when limactl isn’t onPATH.TestConformanceFindschecks the suite itself: each of a set of fakes that breaks the contract in one method fails the check meant to catch it.
The registry’s For chooses a VM’s backend: a VM that exists gets the one its state records in backend=, and a new VM gets backend.Default, which is lima. Tests set the registry’s NewVMs to give new VMs a fake backend (backendtest.Fake). The command line has no way to choose a backend, so a second backend would also add one, following the cli skill, together with its column in the tables that list VMs, images and caches. --json output already names each VM’s, image’s and shared tool’s backend.
A new backend gets a design note and a threat check with the security-audit skill before any of its emulations is marked reviewed, and that check covers what Lima gets from its VMs’ design. A VM started outside devvm must not forward an agent devvm didn’t choose, a VM whose root can change its own network must not count as confined, a secret the proxy needs goes in a private file and never in a command’s arguments, and a socket on this machine goes in a private folder.
Adding a toolchain
Each toolchain is one image.ToolDef entry in image.SharedTools, in internal/image/tool.go, and the rest of devvm reads every list of the toolchains and every fact of one from there: its members in the lock, the files in a project folder that call for it, its installer, the environment that every VM’s shells get, the folders of the VM’s home that go on every VM’s PATH, and the members whose version files mise reads. --tools, devvm tools, the picker, shell completion, the new VM’s summary and the devcontainer.json features that devvm maps list the toolchains from the table too, and every launch writes the environment, the PATH folders and the version files into the VM, so a new toolchain changes no image. A new toolchain that mise installs from the lock’s checked downloads, as it installs node, python and go, needs these files and nothing else:
internal/image/tool.go: its entry, with itsEnv,PathDirsandVersionFileswhen it has them.assets/shared-tools/mise.toml: a line in[tools]for each member, with its hold, and a line in[tool_alias]for each member that isn’t one of mise’s own (core:), naming the backend it comes from.assets/shared-tools/<name>.policy: the hosts its allowlist allows, such as its package registry and the downloads of the versions a project asks for.testdata/mise.lockandtests/e2e-mise.lock: each member’s lock entry, whichscripts/bump.sh e2e-toolswrites into the second.docs/content/docs/using/toolchains.md: its row in the table, in the registry’s order, its section, with what it brings, the files that call for it, its version files, its global installs and its allowlist, and its row in the glibc table of Which bases they run on.tests/e2e/tools_test.go: its checks, such as a project’s VM that its files call for, a global install and a version file’s own version.
Then the golden files in internal/vm/testdata/golden take its environment, its PATH folders and its version files with UPDATE_GOLDEN=1 go test ./internal/vm -run Golden, and a reviewer reads their diff. TestToolchainsComplete, TestToolchainGuest, TestSharedToolsPolicies and TestConfigTools fail until those files match the table, and the structure test fails on a string literal elsewhere that is its name or a member’s.
A toolchain that needs more than mise’s install gets code of its own, which the table can’t hold. A step in each VM, such as rust’s rustup in ~/.cargo, go’s telemetry switch or dotnet’s /etc/dotnet/install_location, is a block of assets/guest/sync.sh that reads the toolchain’s version from the VM’s tool list. An install that mise can’t check, such as rust’s, which rustup checks against its own signed manifest, or dotnet’s, whose SDK archive devvm adds to the lock with its SHA-512, is an Installer of its own, with its branch in assets/shared-tools/build.sh and its handling of the lock in internal/tools.
Adding a coding agent
Each coding agent is one agents.Def in internal/agents/agents.go, and the rest of devvm reads every list of the agents and every fact of one from there: its shared tool (image.SharedTools makes its entry, glibc-only or not), its settings folders and files with each file’s format, its Seeds, its Names and NamedValues, the folder that devvm agent import copies from, which of that folder’s entries are each machine’s own (its FolderOwn), which neither the import nor the save copies and no VM gets, its login files and their kinds, its history and what an export leaves out of it (its logins, and what’s the VM’s own as a machine, its HistoryOwn, which a VM made again keeps), where in that history it makes git worktrees, which devvm rm and a remake with --clean-home check for uncommitted changes first (its Worktrees), the environment that every VM’s shells get, which turns off its self-update and telemetry, the secret every VM with it uses, how devvm agent login logs it in (its Login: the command lines of its own login, with a code and for a setup token, and whether the login comes back to a forwarded port or you paste a code back; or API keys alone), whether config.toml’s [pass-keys] takes it, and its row in the docs’ Supported agents table (its Docs). Its Seeds are the folders it reads in a VM as a seed of a folder of the VM’s own, such as Claude Code’s plugins. Each one has the link that the VM’s home puts the store’s folder at and the entries that devvm agent import and devvm agent save copy, which agents.MachineOwn judges. Its Names and NamedValues are the tables in its settings files whose keys are names you choose, such as its MCP servers’, which agentlogin.Redact doesn’t judge as keys, read from the agent’s own docs. The commands’ help, --agents, the picker, shell completion, the messages and the CLI tests’ lists of the agents all come from the registry too. A new agent that works like one devvm has needs these files and nothing else:
internal/agents/agents.go: itsDefand its name’s constant.assets/shared-tools/mise.toml: its line in[tool_alias], the backend it comes from, and in[tools], its version.assets/shared-tools/<name>.policy: the hosts its allowlist allows.testdata/mise.lockandtests/e2e-mise.lock: its lock entry, whichscripts/bump.sh e2e-toolswrites into the second.docs/content/docs/using/coding-agents.md: a section on its login when it has one.CHANGELOG.md: its entry.tests/e2e/network_test.go: its name in the network section’s--agentslist, which launches one VM with every agent, and the checks of its allowlist, its login and its copies there.assets/guest/sync.sh, only when it needs setup in the VM beyond its settings and logins, as Claude Code’s link in~/.local/binand Codex’s lines in itsconfig.tomldo.- When it keeps a login file: a format for each of its kinds in a file of its own in
internal/agentlogin, named for the agent, with theformatinterface’s methods,kindnaming its kind (andkeyFormatorchatGPTFormatwhen itsDefsays it saves API keys or holds a ChatGPT login, andcaptureFormatwhen the VM’s proxy captures its logins: its token endpoints, the start of itsUser-Agentwhen another agent shares an endpoint, its capture and its reply to the VM’s own refresh), added to the list informatsinformat.go, with its fixture ingoldenLogins(internal/agentlogin/golden_test.go) and its golden file, and its format’s section ininternals/network.md.
Then mise run cli-docs writes the agent’s row in the Supported agents table, its name in the README’s list and the CLI pages whose help lists the agents, and the golden files in internal/vm/testdata/golden take its paths with UPDATE_GOLDEN=1 go test ./internal/vm -run Golden. TestDefsComplete, TestFormatsCoverLogins, TestSharedToolsPolicies, TestConfigTools and TestReferenceIsCurrent fail until those files match the registry. Code of the agent’s own, such as a settings filter or a login of its own, goes in a file named for the agent, such as claude.go or agentlogin_claude.go, and names the agent by its constant in internal/agents, so the code for every agent calls a hook there rather than asking which agent it has. The structure test fails on a string literal elsewhere that is the agent’s name or holds its folder, on code outside the agent’s own files that compares with its constant, has a case for it, looks for it or keys a map on it, on a list in Go that names every agent, tests included, and on a CLI test script’s line that lists agents as a message does, which $AGENT_CHOICES, $LOGIN_AGENTS and $KEY_AGENTS stand in for. The network section of the e2e tests drives every agent, so the new one gets its checks there.
Adding a provider
The model providers whose API keys the VM’s proxy puts in for the agents with a key for each provider, opencode and pi, are one table, agents.Providers in internal/agents/agents.go. Each agents.Provider holds the provider’s ID, which is its entry’s name in both agents’ auth.json, its name as people know it, the hosts the agents send its key to, its Path when the agents send the key below one path of those hosts alone, such as OpenRouter’s /api/, the Headers they send it in, most often one but two for OpenRouter, whose Anthropic models take the key in x-api-key, and its Prefix, which starts the key’s placeholder when an agent checks how the provider’s keys start: pi takes an OpenAI key that doesn’t start with sk- for a Sign in with ChatGPT login, so OpenAI’s is sk-. The opencode and pi formats’ bindings, agentlogin.KeyProviders, which devvm agent login --provider takes and its help lists, and the allowlist lines of each binding’s hosts all come from it. A new provider is its entry there, in the order of its ID, and its row in the table in using/coding-agents.md’s API keys the proxy puts in, which TestProvidersComplete checks against the registry. Read the hosts, the paths, the headers and any check of how a key starts in both agents’ own source first, and in the SDK that each one sends its requests with, because a header that differs from the agents’, or one that an agent uses for some models alone, leaves the key’s placeholder in the request, and a host that the agents never send the key to would get the key anyway. The proxy puts a provider’s key in only when the header’s value is the placeholder alone, or Bearer and the placeholder in Authorization, so check that both agents send the key that way too. A Path starts and ends with /, is clean, and goes into each binding as its Path, below which pathWithin takes a request’s path, so set one only when every request of both agents with the key goes below it. Then mise run cli-docs writes the CLI page whose help lists the providers, and UPDATE_GOLDEN=1 go test ./internal/agentlogin -run Golden writes the golden files that list them.