Files
w3cjandClaude Opus 5.5 b6954e1e11 refactor(vmname): a VM's name is a vmname.VM, parsed where it's read
vmname.VM is the name of every machine devvm runs on a backend: a
project VM, an image's machine (devvm-img-<key>), a build VM and a
remake's aside. Fields, parameters, results and map keys that held a VM's
name as a string now hold a VM, from the backend interface down to the
CLI's --json structs, whose output is byte for byte the same.

Boundary parsers, the only places a string becomes a VM:
- vmname.Parse: a project VM's name (state's vm.json name, export file
  names, devvm _links's argument, the forwarder's -vm, project.Nth)
- vmname.ParseListed: one path element, for what a backend lists
- VM.UnmarshalText: JSON (Lima's list, proxy config and events, links'
  targets in state, link lists); empty stays no VM
- vmname.Image(key), VM.Aside(), and typed constants for Lima's build VMs
- msg.M.VM: the VM a message's function took, read back

vmname's functions on names became methods (Hash, Slug, Short, Aside,
AsideHash, ImageKey, IsProject); Lima's image prefix moved to vmname;
vmname.Join joins VM lists. Messages' vm and vm_name fields are typed
by the generator. Seven messages whose field took something else became
what= (backend_lacks, backend_unreviewed, backend_unreviewed_used,
starting, deleting, configuring, running_script) and rm_vm_failed's
name=: the text is unchanged, the names-mode scripts follow.
Review.RequireFor names an image or the tools being built.

Typed sites per package (added lines with vmname.VM, non-test): msg 347
(generated), vm 124, lima 48, backendtest 32, vmssh 31, netpolicy 22,
backend 16, gitprotect 14, proxy 11, cli 11, project 10, state 8,
ports 6, paths 3, sshagent 2, sshconfig 2, tools 2.

The structure test's owners rule now fails on a field, parameter or
result named for a VM (vm, vms, otherVM, vmName) typed string, []string
or map[string]…, and on converting anything but a literal to vmname.VM
outside the files it lists; internal/stubtest is free. depguard holds
internal/vmname to the standard library, and the proxy may import it.

ParseListed and UnmarshalText refuse a name starting with -, which would read as a flag in limactl's argv, and vmname.Image refuses a key that's empty, holds a / or NUL, or makes a project VM's name, so Lima's ImageSize and DeleteImage no longer check it themselves. The check also fails on a *string VM name and an alias of vmname.VM, and says what it doesn't see.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-15 19:36:28 -07:00

168 lines
6.0 KiB
Go

package vm
import (
"context"
"encoding/json"
"os/exec"
"path/filepath"
"github.com/devvmhq/devvm/internal/backend"
"github.com/devvmhq/devvm/internal/jsonc"
"github.com/devvmhq/devvm/internal/msg"
"github.com/devvmhq/devvm/internal/platform"
"github.com/devvmhq/devvm/internal/safefile"
"github.com/devvmhq/devvm/internal/vmname"
"github.com/devvmhq/devvm/internal/vmssh"
)
// ownSSH is how devvm's own ssh reaches a running VM, for links and a
// --clone VM's fetch: its backend's ssh (reviewed), with the
// VM's own key and the host keys devvm pinned for its current start, read
// as they are (vmssh.Checked, vmssh.ReadKeys).
func (e *Env) ownSSH(ctx context.Context, b backend.Backend, vm vmname.VM) (vmssh.SSH, error) {
r, err := vmssh.Checked(ctx, e.Dirs, e.Registry.Review, b, vm, vmssh.ReadKeys)
return r.SSH, err
}
// ShowHost gets the folder's VM ready for ssh and editors (reach), then
// says the name they reach it by and the folder to open (shellDir).
func (e Launch) ShowHost(ctx context.Context) error {
host, t, err := e.reach(ctx)
if err != nil {
return err
}
e.Say(msg.SSHHostInfo(t.shellDir(ctx, e.vmLog()), host))
return nil
}
// CursorCommand is the Cursor editor's command, which devvm cursor runs
// and is named after: the editor's, not its CLI agent's.
const CursorCommand = "cursor"
// Code opens the folder's VM in VS Code, with its code command (openRemote).
func (e Launch) Code(ctx context.Context) error {
return e.openRemote(ctx, "code", msg.NoCodeCommand, nil)
}
// Cursor opens the folder's VM in the Cursor editor, with its cursor
// command, which takes VS Code's (openRemote), once it finds the command
// first warning when Cursor's Agent will run in the VM
// (cursorAgentWarning).
func (e Launch) Cursor(ctx context.Context) error {
return e.openRemote(ctx, CursorCommand, msg.NoCursorCommand, func() {
if m, warn := cursorAgentWarning(e.Dirs.Home, filepath.Dir(e.Dirs.Config)); warn {
e.Warn(m)
}
})
}
// cursorRoute is the setting, in Cursor's user settings, that sends its
// Agent's requests in a Remote-SSH window through the Cursor app on this
// machine when it's cursorRouteAlways; recent Cursor runs Agent in the
// VM's cursor-server otherwise. Cursor reads it only from the user
// settings (it's application-scoped), never from the VM's.
const cursorRoute, cursorRouteAlways = "cursorAgentHost.remoteInferenceRoute", "always"
// cursorUserSettings is Cursor's user settings file, in home or, on
// Linux, the XDG config folder configHome (platform.AppSettingsDir).
func cursorUserSettings(home, configHome string) string {
return filepath.Join(platform.AppSettingsDir(home, configHome), "Cursor", "User", "settings.json")
}
// cursorSettingsMax is the most of Cursor's user settings devvm cursor
// reads.
const cursorSettingsMax = 1 << 20
// cursorAgentWarning reads Cursor's user settings on this machine, which
// it never writes, and returns the warning devvm cursor gives when they
// don't set cursorRoute to cursorRouteAlways, can't be read, aren't a
// regular file of at most cursorSettingsMax (safefile.Read: never a FIFO
// or a device) or aren't JSON with comments, naming the file.
func cursorAgentWarning(home, configHome string) (msg.M, bool) {
file := cursorUserSettings(home, configHome)
warn := msg.CursorAgentInVM(file)
data, err := safefile.Read(file, cursorSettingsMax, safefile.Opts{Follow: true})
if err != nil {
return warn, true
}
var settings map[string]any
if json.Unmarshal(jsonc.Strip(data), &settings) != nil || settings[cursorRoute] != cursorRouteAlways {
return warn, true
}
return msg.M{}, false
}
// openRemote gets the folder's VM ready for ssh and editors (reach), then
// opens the folder its shell would open in (shellDir) over Remote-SSH with
// an editor's command, in devvm's place, with the environment devvm
// started with: the SSH agent devvm's own connections use isn't the
// editor's. Without the command on PATH, it fails with missing, naming the
// host to connect to; with it, found runs first, unless it's nil.
func (e Launch) openRemote(ctx context.Context, command string, missing func(host string) msg.M, found func()) error {
host, t, err := e.reach(ctx)
if err != nil {
return err
}
path, err := exec.LookPath(command)
if err != nil {
return missing(host)
}
if found != nil {
found()
}
return e.Exec(backend.Command{
Path: path,
Args: []string{command, "--remote", "ssh-remote+" + host, t.shellDir(ctx, e.vmLog())},
Env: e.Environ,
})
}
// reach launches the folder's VM as devvm does without its shell
// (launch), sets it up, checks its backend says how ssh reaches it, and
// makes sure ~/.ssh/config includes devvm's ssh config, asking first. It
// returns the name ssh reaches it by.
func (e Launch) reach(ctx context.Context) (string, *target, error) {
t, err := ready(ctx, e)
if err != nil {
return "", nil, err
}
if _, err := e.sshConfig().EnsureInclude(e.Dirs.Home); err != nil {
return "", nil, err
}
return t.sshHost(), t, nil
}
// ready launches the folder's VM as devvm does without its shell (launch),
// sets it up, which puts it in devvm's ssh config, and checks its backend
// says how ssh reaches it.
func ready(ctx context.Context, e Launch) (*target, error) {
t, _, err := readyConf(ctx, e)
return t, err
}
// readyConf is ready, with the VM's confinement as its launch gave it,
// which setting it up again needs (syncer.sync).
func readyConf(ctx context.Context, e Launch) (*target, backend.Confinement, error) {
var none backend.Confinement
t, err := e.finder.target(ctx, need{has: hasMade})
if err != nil {
return nil, none, err
}
if err := e.Registry.Require(t.b, t.f.Name, backend.FeatureSSH); err != nil {
return nil, none, err
}
conf, err := e.launch(ctx, t, upOpts{})
if err != nil {
return nil, none, err
}
if err := e.setUpVM(ctx, t, conf); err != nil {
return nil, none, err
}
// A VM running since a start without its ssh (an emulation not yet
// let through then) has none to reach.
if _, err := t.b.SSH(ctx, t.f.Name); err != nil {
return nil, none, err
}
return t, conf, nil
}