mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
Lock waits: - platform.LockFile and LockFileShared take a platform.Waiting, called once they find the lock held, never when it's free, whose end they call once the wait ends; LockFileWaiting, LockFileSharedWaiting and their delays (ShareNote, OCINote, WaitNote, groupsNote, makingNote) go. - msg.Waiting makes one from a Reporter's Status and a label; msg.WithStatus and msg.StatusOf carry the run's Status in its context for the two packages that take no Reporter, internal/state and internal/trust, which cli's execute sets; forbidigo keeps StatusOf to them, and AGENTS.md says so. - Every lock that waits shows its own label: vm_state_waiting (a VM's state, its deletion), claims_waiting, layout_waiting, git_lock_waiting, ca_waiting, packages_waiting, rebuilds_waiting, ubuntu_pin_waiting, custom_secrets_waiting, forwarder_waiting, agent_store_waiting, and the waits that printed a line after a delay, which now show theirs as a status line: share_waiting, oci_waiting, image_build_waiting, group_waiting, making_waiting. - An agent's login file's lock (agentlogin's lockLogin, a folder's flock that a VM's proxy holds while it refreshes the login) tries first without waiting and shows agentlogin.File's Waiting (agent_login_waiting) while it waits; secrets fills it from its Reporter, for SaveLogins, Claude Code's logins folder and every File. - The structure test's status-lines rule counts a LockFile or LockFileShared whose waiting is nil as slow work; the build port's record, a file just made that never waits, is listed in statusSites. - Without a terminal, or with -v, a status line draws nothing, so the waits that printed an info line after a delay no longer print it there; the CHANGELOG and the troubleshooting page say so. from.txtar's check that a free conversion lock printed no wait goes, as TestLockOCI checks it. The layout's migration runs under a status line (layout_migrating), only when there are migrations to run. Backend calls outside a launch: TestCommandStatusLines runs devvm ls, ports, size, network, tools, git-protect, history export, stop and rm on a running and a stopped VM and fails on a backend call with no status line showing, with a control that each makes one. A project folder devvm can't open and read (vm_folder_unreadable) was taken for another folder's VM (vm_other_folder, which offers rm and adopt, and adopt went on); the finder now keeps why (target.unread), and every command on the folder's VM, from the folder or a subfolder of it, names the folder and why; devvm rm VM run elsewhere goes by the VM's name as before. platform.UniqueFolderID had no caller but its test: FolderStat.Unique, from the open folder, replaced it; it goes, and its test checks StatFolder's Unique instead. Tests: TestLockFileWaiting (rewritten: called only when held, its end once got or given up, shared too), TestVMLockWaits, TestCheckLayoutWaits, TestCheckLayout (migrations under layout_migrating, nothing shown otherwise), TestLockGroups, TestLockShare, TestMaking, TestLockOCI and the build lock's test (status lines, not info lines), TestLockLogin, TestInitCACancelled (ca_waiting), the status-lines rule's lock cases, TestCommandStatusLines, TestUnreadableFolder (a subfolder of it too), TestStatFolderUnique. Docs: Contributing's CLI page (Status lines) and structure page, the cli and codebase-standards skills (the guards), AGENTS.md's Go conventions, internals/images.md and design.md (the waits), using/troubleshooting.md, using/vms.md (a folder devvm can't read); CHANGELOG. Co-Authored-By: Claude Opus 5.5 <[email protected]>
135 lines
4.2 KiB
Go
135 lines
4.2 KiB
Go
package state
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/paths"
|
|
"github.com/devvmhq/devvm/internal/platform"
|
|
"github.com/devvmhq/devvm/internal/vmname"
|
|
)
|
|
|
|
// Layout is the layout version this devvm keeps its folders in: which
|
|
// folders and files the data and config folders hold, and their formats.
|
|
// A change to the layout that an older devvm would misread raises it,
|
|
// with a migration (migrations) from the version before.
|
|
const Layout = 1
|
|
|
|
// Migration changes devvm's folders from one layout version to the next.
|
|
type Migration func(paths.Dirs) error
|
|
|
|
// migrations are the migrations from each layout version to the next:
|
|
// the first takes version 1 to 2, so there are Layout-1 of them.
|
|
func migrations() []Migration { return nil }
|
|
|
|
// CheckLayout checks the layout versions of devvm's data and config
|
|
// folders (paths.Dirs.Version, ConfigVersion) against this devvm's, at a
|
|
// run's start, under the version lock: the same version goes on, an
|
|
// older one is migrated to this one, and a newer one is refused with
|
|
// layout_newer, because a newer devvm changed the folders. A folder
|
|
// without a version file gets this devvm's: nothing older has one. It
|
|
// waits for the lock, and migrates, under ctx's status line (msg.StatusOf).
|
|
func CheckLayout(ctx context.Context, d paths.Dirs) error {
|
|
return checkLayout(ctx, d, Layout, migrations())
|
|
}
|
|
|
|
func checkLayout(ctx context.Context, d paths.Dirs, current int, ms []Migration) error {
|
|
if len(ms) != current-1 {
|
|
return fmt.Errorf("layout version %d has %d migrations", current, len(ms))
|
|
}
|
|
if err := PrivateDir(filepath.Dir(d.VersionLock())); err != nil {
|
|
return err
|
|
}
|
|
status := msg.StatusOf(ctx)
|
|
unlock, err := platform.LockFile(ctx, d.VersionLock(), msg.Waiting(status, msg.LayoutWaiting()))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer unlock()
|
|
files := []string{d.Version(), d.ConfigVersion()}
|
|
from := current
|
|
stale := false
|
|
for _, f := range files {
|
|
v, err := readVersion(f)
|
|
switch {
|
|
case errors.Is(err, fs.ErrNotExist):
|
|
stale = true
|
|
continue
|
|
case err != nil:
|
|
return err
|
|
case v > current:
|
|
return msg.LayoutNewer(strconv.Itoa(current), f, strconv.Itoa(v))
|
|
}
|
|
stale = stale || v < current
|
|
from = min(from, v)
|
|
}
|
|
if !stale {
|
|
return nil
|
|
}
|
|
if len(ms[from-1:]) > 0 && status != nil {
|
|
defer status(msg.LayoutMigrating())() // a migration may move every VM's folder
|
|
}
|
|
for _, m := range ms[from-1:] {
|
|
if err := m(d); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
data := []byte(strconv.Itoa(current) + "\n")
|
|
for _, f := range files {
|
|
if err := writeFile(f, data); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// readVersion reads a layout version file: a whole number from 1 up. A
|
|
// missing file is an error that matches fs.ErrNotExist.
|
|
func readVersion(path string) (int, error) {
|
|
data, err := os.ReadFile(path) //nolint:gosec // paths come from paths.Dirs
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
v, err := strconv.Atoi(strings.TrimSpace(string(data)))
|
|
if err != nil || v < 1 {
|
|
return 0, &CorruptError{Path: path, Reason: "not a layout version"}
|
|
}
|
|
return v, nil
|
|
}
|
|
|
|
// VMFolderProblems are the entries in the VMs' folder that aren't VMs'
|
|
// folders (vms_stray): anything not a folder named like a hash
|
|
// (vmname.IsHash); and the VMs' folders without a state file
|
|
// (vm_folder_stateless), which a devvm that stopped while making or
|
|
// deleting a VM leaves; and the VMs' folder itself when it can't read it
|
|
// (vms_unreadable), with why. A VM's folder it can't read is
|
|
// vm.StateProblems', so doctor warns of it once. It changes nothing.
|
|
func VMFolderProblems(d paths.Dirs) []msg.M {
|
|
es, err := os.ReadDir(d.VMs())
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return nil
|
|
} else if err != nil {
|
|
return []msg.M{msg.VMsUnreadable(d.VMs()).Because(err)}
|
|
}
|
|
var ms []msg.M
|
|
for _, e := range es {
|
|
path := filepath.Join(d.VMs(), e.Name())
|
|
if !e.IsDir() || !vmname.IsHash(e.Name()) {
|
|
ms = append(ms, msg.VMsStray(path, d.VMs()))
|
|
continue
|
|
}
|
|
// A folder whose vm.json can't be looked at is vm.StateProblems', which names the VM and why.
|
|
if _, err := os.Lstat(paths.VM{Dir: path}.State()); errors.Is(err, fs.ErrNotExist) {
|
|
ms = append(ms, msg.VMFolderStateless(path))
|
|
}
|
|
}
|
|
return ms
|
|
}
|