mirror of
https://github.com/devvmhq/devvm.git
synced 2026-12-01 06:23:17 -10:00
readClone stopped keeping the VM's answer past cloneStateMax (8 MiB), but took the rest and waited for the command to end, with no time limit, so a VM that wrote without end, or never answered, kept devvm rm and devvm clone off waiting until Ctrl+C. The read of the agents' worktrees already had the bounds; workCheck.answer is now the one read for both, taken from agentTrees: at most cloneStateMax bytes, past which the bounded writer cancels the command, within answerWait (2 minutes, treesWait before), an interrupt returned as ctx's error. A failed, over-long or late answer is the check's failure, clone_check_failed or trees_check_failed, which keeps the VM and names the flag that skips the check, and a hostile one is parseCloneState's, as before. readClone fails before parsing on a bad answer, so its result is never a nil list (the nilaway baseline loses that line) and gosec sees its loop bounded by both lists. On Lima the time limit didn't hold for either read: a cancelled limactl shell leaves its ssh child holding the command's output pipes, so os/exec's Wait waited for the VM's command to end. Exec now goes through the lima package's bounded (WaitDelay, a second), as the remount's commands do, so it ends a second after its context does; output a child held past limactl's own success is exec.ErrWaitDelay, an error, so an answer cut short is never taken as a whole one. That bounds every Exec, devvm paste's time limit and the history export included. treeGlobs returns a list that's never nil: nilaway took its nil for one parseAgentTrees slices into, a false positive (agentTrees returns before it without patterns, and parseAgentTrees indexes pats only below nextRecord's len(pats)), which the CI job reported on main. Tests that fail without the fix: TestWorkCheckBounded (TestAgentTreesBounded, now a table over both reads: an endless answer cancelled, a silent VM given up on after 2 minutes in a synctest bubble, an interrupt kept as the interrupt), whose clone case isn't cancelled and then deadlocks its bubble; TestExecHeldOutput, a real limactl stand-in that leaves a child holding its output, killed and after a success, which waits 30 s each, the success read as one, without bounded. TestParseAgentTrees checks treeGlobs with no patterns. Docs: clone's --no-clone-check names a VM that doesn't answer within 2 minutes, How it works' deleting a VM gives the clone read's bounds, the Lima page's A command's output, the clone design note's cost and threat drop the wait that's gone, and internal/vm's map names answer. The changelog gets an If you use --clone block with the line, the release page its section under Fixes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
237 lines
7.6 KiB
Go
237 lines
7.6 KiB
Go
package vm
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"path"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/devvmhq/devvm/internal/agents"
|
|
"github.com/devvmhq/devvm/internal/image"
|
|
"github.com/devvmhq/devvm/internal/msg"
|
|
"github.com/devvmhq/devvm/internal/state"
|
|
"github.com/devvmhq/devvm/internal/tools"
|
|
)
|
|
|
|
// treePattern is where one of a VM's agents makes git worktrees
|
|
// (agents.Def.Worktrees): the agent's name as people know it, and the
|
|
// pattern of each worktree's folder, relative to the home folder.
|
|
type treePattern struct {
|
|
agent, pattern string
|
|
}
|
|
|
|
// treePatterns are the Worktrees of the agents among st's shared tools,
|
|
// in their order: none for a VM whose creation didn't finish, which
|
|
// never ran.
|
|
func treePatterns(st *state.VM) []treePattern {
|
|
if !st.Finished() {
|
|
return nil
|
|
}
|
|
var out []treePattern
|
|
for _, n := range image.AgentsOf(tools.Names(st)) {
|
|
d := agents.Of(n)
|
|
for _, p := range d.Worktrees {
|
|
out = append(out, treePattern{agent: d.Display, pattern: p})
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// treeGlobs are the shell's globs for pats, in order, and the pattern each
|
|
// stands for (of), whose index a record of agentTreesState names: each *
|
|
// of a pattern three ways, *, .[!.]* and ..?*, as a shell's * never
|
|
// matches a name that starts with a dot, which path.Match's does, so a
|
|
// worktree whose name does is found too. of is never nil, so what reads
|
|
// a record by its index (parseAgentTrees) holds a list, even an empty one.
|
|
func treeGlobs(pats []treePattern) (globs []string, of []treePattern) {
|
|
of = make([]treePattern, 0, 3*len(pats))
|
|
for _, p := range pats {
|
|
gs := []string{""}
|
|
for i, part := range strings.Split(p.pattern, "/") {
|
|
ways := []string{part}
|
|
if part == "*" {
|
|
ways = []string{"*", ".[!.]*", "..?*"}
|
|
}
|
|
var next []string
|
|
for _, g := range gs {
|
|
for _, w := range ways {
|
|
if i > 0 {
|
|
w = g + "/" + w
|
|
}
|
|
next = append(next, w)
|
|
}
|
|
}
|
|
gs = next
|
|
}
|
|
for _, g := range gs {
|
|
globs, of = append(globs, g), append(of, p)
|
|
}
|
|
}
|
|
return globs, of
|
|
}
|
|
|
|
// agentTree is a git worktree an agent made in the VM, as agentTreesState
|
|
// gives it: the agent's name, its folder relative to the VM's home
|
|
// folder, its git status, the commit its HEAD is detached at when
|
|
// nothing else the VM has has it (head), and whether its git failed
|
|
// (failed), as when this repo pruned the worktree.
|
|
type agentTree struct {
|
|
agent, path string
|
|
status []byte
|
|
head string
|
|
failed bool
|
|
}
|
|
|
|
// agentTreesState is the script agentTrees runs in the VM, in its home
|
|
// folder: how many worktrees to look at (cloneWorktreesMax), then each
|
|
// glob of treeGlobs, which the shell expands, as the import's clean-up
|
|
// does. For each folder a glob finds with a .git in it, it
|
|
// prints records as cloneState does, "KIND INDEX LENGTH" and that many
|
|
// bytes, INDEX the glob's: path (relative to the home folder), whead
|
|
// (the commit its HEAD is detached at, when no branch, tag or
|
|
// remote-tracking branch the VM has has it), then wstatus or wfail.
|
|
// Past the most, it prints more and stops. The VM can hide anything from
|
|
// what it's asked, so the host checks what it says, not that it says
|
|
// everything.
|
|
const agentTreesState = `cd ~ || exit 1
|
|
lim=$1
|
|
shift
|
|
t=$(mktemp -d) || exit 1
|
|
trap 'rm -rf -- "$t"' EXIT
|
|
put() { printf '%s %s %s\n' "$1" "$i" "$(wc -c < "$t/o" | tr -d ' ')" && cat -- "$t/o"; }
|
|
IFS=
|
|
i=0 k=0
|
|
for p; do
|
|
for w in $p; do
|
|
[ -d "$w" ] && [ -e "$w/.git" ] || continue
|
|
k=$((k+1))
|
|
if [ "$k" -gt "$lim" ]; then : > "$t/o"; put more; exit 0; fi
|
|
printf '%s' "$w" > "$t/o"; put path
|
|
if ! git -C "$w" symbolic-ref -q HEAD >/dev/null 2>&1 &&
|
|
[ -n "$(git -C "$w" rev-list -n1 HEAD --not --branches --tags --remotes 2>/dev/null)" ]; then
|
|
git -C "$w" rev-parse -q --verify HEAD > "$t/o" && put whead
|
|
fi
|
|
if git -C "$w" status --porcelain > "$t/o" 2>/dev/null; then put wstatus; else : > "$t/o"; put wfail; fi
|
|
done
|
|
i=$((i+1))
|
|
done`
|
|
|
|
// agentTrees reads the agents' worktrees that pats find in t's running
|
|
// VM (agentTreesState), in one command, as answer bounds it, and
|
|
// shows each one's work that would be lost, at most cloneShown changes in
|
|
// all: its uncommitted changes, the commits of its detached HEAD that
|
|
// nothing else in the VM has, and a worktree whose git failed, or more
|
|
// worktrees than devvm checks, which may hold either. It reports whether
|
|
// there's any. Failing, or an answer devvm can't take, is its failure
|
|
// (trees_check_failed), as the VM may hold work devvm can't see.
|
|
func (e workCheck) agentTrees(ctx context.Context, t *target, pats []treePattern) (bool, error) {
|
|
if len(pats) == 0 {
|
|
return false, nil
|
|
}
|
|
globs, of := treeGlobs(pats)
|
|
argv := append([]string{"sh", "-c", agentTreesState, "sh", strconv.Itoa(cloneWorktreesMax)}, globs...)
|
|
out, ok, err := e.answer(ctx, t, argv)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
var trees []agentTree
|
|
more := false
|
|
if ok {
|
|
trees, more, err = parseAgentTrees(out, of)
|
|
}
|
|
if !ok || err != nil {
|
|
return false, msg.TreesCheckFailed(e.command, t.f.Name)
|
|
}
|
|
return e.showTrees(trees, more), nil
|
|
}
|
|
|
|
// showTrees shows, for agentTrees, each of trees with work it would lose
|
|
// under its heading, at most cloneShown changes in all, and more, and
|
|
// reports whether there's any.
|
|
func (e workCheck) showTrees(trees []agentTree, more bool) bool {
|
|
shown, lost := 0, more
|
|
for _, w := range trees {
|
|
at := "~/" + w.path
|
|
switch {
|
|
case w.failed:
|
|
e.Say(msg.AgentTreeUnread(w.agent, at))
|
|
lost = true
|
|
case len(bytes.TrimSpace(w.status)) > 0:
|
|
lost = true
|
|
if shown < cloneShown {
|
|
e.Say(msg.AgentTreeDirty(w.agent, at))
|
|
shown += e.showChanges(w.status, cloneShown-shown)
|
|
}
|
|
}
|
|
if w.head != "" {
|
|
e.Say(msg.AgentTreeDetached(w.agent, at))
|
|
lost = true
|
|
}
|
|
}
|
|
if more {
|
|
e.Say(msg.AgentTreesUnchecked(strconv.Itoa(cloneWorktreesMax)))
|
|
}
|
|
return lost
|
|
}
|
|
|
|
// errTreesState is parseAgentTrees' error: the VM's answer isn't what
|
|
// agentTreesState prints.
|
|
var errTreesState = errors.New("the agents' worktrees' answer isn't agentTreesState's")
|
|
|
|
// parseAgentTrees reads agentTreesState's records for pats. The VM's
|
|
// answer is untrusted: each record must be whole and of a pattern asked
|
|
// for, each path a clean relative path that its pattern matches, each
|
|
// once and at most cloneWorktreesMax, followed by one status, and more
|
|
// comes last.
|
|
func parseAgentTrees(out []byte, pats []treePattern) ([]agentTree, bool, error) {
|
|
var trees []agentTree
|
|
seen := map[string]bool{}
|
|
var cur *agentTree
|
|
at, more := 0, false
|
|
r := bufio.NewReader(bytes.NewReader(out))
|
|
for {
|
|
kind, i, body, err := nextRecord(r, len(pats))
|
|
if errors.Is(err, errNoRecord) {
|
|
break
|
|
}
|
|
// Nothing follows more, and a worktree's records are all its
|
|
// pattern's.
|
|
if err != nil || more || cur != nil && (i != at || kind == "path" || kind == "more") {
|
|
return nil, false, errTreesState
|
|
}
|
|
switch kind {
|
|
case "path":
|
|
p := string(body)
|
|
ok, _ := path.Match(pats[i].pattern, p)
|
|
if !ok || path.Clean(p) != p || seen[p] || len(trees) == cloneWorktreesMax {
|
|
return nil, false, errTreesState
|
|
}
|
|
seen[p], at = true, i
|
|
cur = &agentTree{agent: pats[i].agent, path: p}
|
|
case "whead":
|
|
sha := string(bytes.TrimSuffix(body, []byte("\n")))
|
|
if cur == nil || cur.head != "" || !objectName.MatchString(sha) {
|
|
return nil, false, errTreesState
|
|
}
|
|
cur.head = sha
|
|
case "wstatus", "wfail":
|
|
if cur == nil {
|
|
return nil, false, errTreesState
|
|
}
|
|
cur.status, cur.failed = body, kind == "wfail"
|
|
trees, cur = append(trees, *cur), nil
|
|
case "more":
|
|
more = true
|
|
default:
|
|
return nil, false, errTreesState
|
|
}
|
|
}
|
|
if cur != nil {
|
|
return nil, false, errTreesState
|
|
}
|
|
return trees, more, nil
|
|
}
|