# 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.Backend`** and returns its errors instead of ending the run: a `msg.M`, or `backend.Logged(m)` once a logged command failed, so the command shows the run log's end before the message. `Backend` is the backend's `Name` and `Caps` and four roles, each an interface of its own that every backend implements in full: - `backend.Host` is what the backend needs of this machine and where it keeps its VMs there. `Problems` lists what the host lacks for it, and `backend.Check(ctx, b)` returns the first of them, so a backend doesn't write a check of its own. `NameMax` is the longest VM name it takes, `Location` is where its VMs live when that can change between runs (`backend.LocationOf`), and `DiskDirs` and `Floors` are the folders its disks are in and the free space it wants there (`backend.Space`). A backend whose VMs can't move returns `""` from `Location`, and one whose disks aren't on this machine returns no `DiskDirs`. - `backend.Images` builds images and the shared tools' images and keeps the images. `BuildImage` runs the spec's `Recipe` (`image.Recipe`, which `internal/build` puts together and `image.Input` hashes) as it is, each `image.Step` with its arguments and as root when it says so. `Origin` is the backend's own steps that base starts with, such as the Ubuntu image Lima pinned on this machine, a `--from` image's conversion, base's disk size (`backend.BaseDiskGiB`) and `base.yaml`. Base's size is `backend.BaseCPUs`, `backend.BaseMemoryGiB` and `backend.BaseDiskGiB`, never read from a backend's files. `internal/build` puts them first in base's recipe, as its `Origin`, so base's key hashes them, and `BuildImage` is given them back and runs those rather than reading its own files again. `PinOrigin` pins what base starts from on this machine, which `internal/build` asks for before it keys a build (`backend.PinFirst`): on Lima, that's the newest Ubuntu cloud image (`state.UbuntuPin`, found by `ubuntu.Finder`). A base build's `ImageSpec.Packages`, a `backend.Packages`, sends its packages into the VM's `backend.PackagesDir` before the steps and takes them out after, which `internal/build/packages.go` records or replays and checks, and a step that exits `backend.ExitUnreachable` fails as `build_sources_unreachable`. `DeleteToolsLeft` deletes the throwaway VMs that `BuildTools` left, 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.Machines` makes, starts, stops, renames and deletes VMs, lists them, and runs commands and shells in them. A stopped VM changes only at `Start`, which first checks with `backend.Validate` that it can apply the VM's `backend.Want`, such as a disk no smaller than the VM's own, and then applies what the `Want` asks where it differs from the VM. `Start` leaves 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, `Start` owns the config's `env:`, `propagateProxyEnv`, `dns` and `hostResolver`, and a project VM's size, agent mounts and `forwardAgent`, which it compares with the VM's before its one edit ([what devvm sets on a VM](../internals/lima.md#what-devvm-sets-on-a-vm)). Ports are never the backend's: `vmssh.Forwards` forwards them over the backend's `SSH`. `Running` says how a VM runs now, in one `backend.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, and `SSH` says how ssh, links and devvm's forwarder (`vmssh.Forwards`) reach a running VM's sshd, a target that `vmssh.FromTarget` checks. 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`, with `vmssh.FromTarget` ([what each backend emulates](../internals/backends.md#what-each-backend-emulates)). All three get it through `vmssh.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.UnusableError` and one without host keys with `vmssh.ErrNoHostKeys`, which the ssh config skips on. `Exec` passes its `backend.ExecOpts` streams 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 (`TestExecStreamsAsGiven` on Lima). - `backend.Housekeeping` is what devvm space and devvm gc ask: `Usage`, `Strays`, and `DevvmCache` and `DeleteCache` for its own caches. A backend that can't tell returns none. devvm gc also asks `backend.Images` for `DeleteToolsLeft`, which sits beside `BuildTools`, whose VMs it deletes. `backend.Forgetter` is 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`, `Caps` and the helpers its roles share, and it embeds one type for each role, `host`, `images`, `machines` and `housekeeping`, each over the `Lima` it's part of. `backendtest.Fake` is 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 of `backend.Features` whether it has the feature natively, emulates it, or lacks it ([features](../internals/backends.md#features)). It lists every feature by hand, `None` included, and `Registry.Register` refuses `Caps` that leave one out or rate a feature better than the one `Feature.BuiltOn` says it's built on (`Caps.Check`). The callers, not the backend, decide what a VM needs, and they check it with the registry's `Review.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](design.md), 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 besides `internal/cli` that imports a backend. The registry hands the backend out behind its guard, which checks every `CreateVM`, `Start`, `BuildImage` and `BuildTools` call before the backend sees it. The guard refuses a spec that needs a feature `Review.Require` would refuse, using the specs' own `Features`, and a build spec whose `ImageSpec.Validate` or `ToolsSpec.Validate` fails, such as one with a network that isn't a build's or a key that isn't one. So a caller that forgets `Require` still 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 a `backend.Images` or a `backend.Machines` is still guarded. The guard's type has only `backend.Backend`'s methods, so code that looks for an optional role, such as `backend.Forgetter`, uses `backend.As` instead 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 that `backend.Backend`'s documentation gives. The backend's tests call it with a factory that returns a new `backendtest.Subject` for each check, and each check runs as a subtest. The subject is the backend, holding one image, whose key is `Image`, and no VMs. `Dir` is a folder of the check's own for the folders its VMs mount, `Boots` says whether its VMs start, and `Restart` starts a running VM again outside `Start`, 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 is `backend.ErrNotFound`. It also checks that `Running` gives a stopped VM its lifeline with an empty `ID`, that every start gets another `ID`, that a VM is `Confined` only while it runs on a start `Start` made with `NetProxy`, and that the registry's guard still refuses what the backend lacks when the backend is used as `backend.Images` or `backend.Machines` alone. `checkProxyURL` checks what the open tier needs of every backend, which has no feature of its own ([features](../internals/backends.md#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`). When `Boots` is false, the checks that need a running VM are skipped with the reason, and when `Restart` is nil, so is the one that needs it. `backendtest.Fake` runs the suite in `internal/backend/backendtest`, and `TestConformance` in `internal/backend/lima` runs 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 on `PATH`. `TestConformanceFinds` checks 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](https://github.com/devvmhq/devvm/blob/main/.agents/skills/cli/SKILL.md), 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 its `Env`, `PathDirs` and `VersionFiles` when 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/.policy`: the hosts its allowlist allows, such as its package registry and the downloads of the versions a project asks for. - `testdata/mise.lock` and `tests/e2e-mise.lock`: each member's lock entry, which `scripts/bump.sh e2e-tools` writes 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`: its `Def` and 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/.policy`: the hosts its allowlist allows. - `testdata/mise.lock` and `tests/e2e-mise.lock`: its lock entry, which `scripts/bump.sh e2e-tools` writes 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 `--agents` list, 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/bin` and Codex's lines in its `config.toml` do. - 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 the `format` interface's methods, `kind` naming its kind (and `keyFormat` or `chatGPTFormat` when its `Def` says it saves API keys or holds a ChatGPT login, and `captureFormat` when the VM's proxy captures its logins: its token endpoints, the start of its `User-Agent` when another agent shares an endpoint, its capture and its reply to the VM's own refresh), added to the list in `formats` in `format.go`, with its fixture in `goldenLogins` (`internal/agentlogin/golden_test.go`) and its golden file, and its format's section in `internals/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.