| @@ -1,53 +1,38 @@ |
| 1 | 1 | #!/usr/bin/env bash |
| 2 | 2 | # |
| 3 | 3 | # agent-vm: Run AI coding agents inside sandboxed Lima VMs |
| 4 | | −删除:# Part of https://github.com/sylvinus/agent-vm |
| 5 | | −删除:# |
| 6 | | −删除:# Source this file in your shell config: |
| 7 | | −删除:# source /path/to/agent-vm/agent-vm.sh |
| 8 | | −删除:# |
| 9 | | −删除:# Usage: |
| 10 | | −删除:# agent-vm setup - Create the base VM template (run once) |
| 11 | | −删除:# agent-vm claude - Run Claude Code in a persistent VM for cwd |
| 12 | | −删除:# agent-vm opencode - Run OpenCode in a persistent VM for cwd |
| 13 | | −删除:# agent-vm codex - Run Codex CLI in a persistent VM for cwd |
| 14 | | −删除:# agent-vm vibe - Run Mistral Vibe in a persistent VM for cwd |
| 15 | | −删除:# agent-vm shell - Open a shell in the persistent VM for cwd |
| 16 | | −删除:# (alias: 'sh'; add -c "..." for a one-shot command) |
| 17 | | −删除:# agent-vm stop - Stop the VM for cwd |
| 18 | | −删除:# agent-vm rm - Stop and delete the VM for cwd |
| 19 | | −删除:# agent-vm list - List all agent-vm VMs |
| 20 | | −删除:# agent-vm status - Show status of all VMs (current dir marked with >) |
| 21 | | −删除:# agent-vm name - Print the VM name for cwd |
| 22 | | −删除:# agent-vm info - Print machine-readable state (key=value) |
| 23 | | −删除:# agent-vm version - Print the agent-vm version |
| 24 | | −删除:# agent-vm help - Show help |
| 4 | +新增:# Part of https://www.agent-vm.org/ |
| 25 | 5 | # |
| 6 | +新增:# Put it on your PATH with `./agent-vm.sh install`. It can also be sourced |
| 7 | +新增:# from bash, which defines agent-vm as a shell function. Commands: |
| 8 | +新增:# `agent-vm help` (lib/help.sh). |
| 9 | +新增: |
| 10 | +新增:# zsh, which 0.1.0's installer had source this file from ~/.zshrc: only a |
| 11 | +新增:# function that runs this file with bash. %x is the file being sourced, :A |
| 12 | +新增:# makes it absolute with links resolved. The eval keeps bash from parsing zsh |
| 13 | +新增:# syntax. |
| 14 | +新增:if [[ -n "${ZSH_VERSION:-}" ]]; then |
| 15 | +新增: eval ' |
| 16 | +新增: [[ "${ZSH_EVAL_CONTEXT:-}" == *:file* ]] || exec bash "$0" "$@" |
| 17 | +新增: eval "agent-vm() { command bash ${(qq)${(%):-%x}:A} \"\$@\"; }" |
| 18 | +新增: ' |
| 19 | +新增: return 0 |
| 20 | +新增:fi |
| 26 | 21 | |
| 27 | 22 | # Semantic version of this file. Bumped by hand on release. Integrators gate on |
| 28 | 23 | # it via `agent-vm version`; a build with no `version` command predates it. |
| 29 | | −删除:AGENT_VM_VERSION="0.1.0" |
| 24 | +新增:AGENT_VM_VERSION="0.2.0" |
| 30 | 25 | |
| 31 | 26 | AGENT_VM_TEMPLATE="agent-vm-base" |
| 32 | | −删除:AGENT_VM_STATE_DIR="${HOME}/.agent-vm" |
| 27 | +新增:# Overridable, for a test or a second install, without moving HOME (and |
| 28 | +新增:# Lima's state with it). `agent-vm info` publishes it as state_dir=. |
| 29 | +新增:AGENT_VM_STATE_DIR="${AGENT_VM_STATE_DIR:-${HOME}/.agent-vm}" |
| 33 | 30 | |
| 34 | | −删除:# Directory holding the real agent-vm.sh, symlinks followed. |
| 35 | | −删除:# |
| 36 | | −删除:# ./install.sh puts a symlink on PATH (~/.local/bin/agent-vm -> <repo>/agent-vm.sh) |
| 37 | | −删除:# so `agent-vm` works as an ordinary command. Without resolving the link, this |
| 38 | | −删除:# would point at ~/.local/bin and `agent-vm setup` would fail to find |
| 39 | | −删除:# agent-vm.setup.sh, which lives next to the real file. |
| 40 | | −删除:# |
| 41 | | −删除:# `readlink -f` would do it in one call but is GNU-only — macOS ships a readlink |
| 42 | | −删除:# without it — so walk the chain by hand. |
| 43 | | −删除:# |
| 44 | | −删除:# `CDPATH=` because `dirname` can yield a bare relative path (running |
| 45 | | −删除:# `bash sub/agent-vm.sh`). With CDPATH set, `cd <relative>` searches it before |
| 46 | | −删除:# the current directory and prints where it landed, so without clearing it this |
| 47 | | −删除:# would resolve the wrong directory and capture a stray line — leaving |
| 48 | | −删除:# `agent-vm setup` unable to find agent-vm.setup.sh next to the real file. |
| 31 | +新增:# Directory of the real agent-vm.sh, symlinks followed: the PATH entry |
| 32 | +新增:# `install` makes is one. By hand, as macOS's readlink has no -f. `CDPATH=` |
| 33 | +新增:# everywhere a relative path is cd'ed to: with it set, cd searches it first. |
| 49 | 34 | _agent_vm_script_dir() { |
| 50 | | −删除: local src="${BASH_SOURCE[0]:-$0}" dir |
| 35 | +新增: local src="$1" dir |
| 51 | 36 | while [ -L "$src" ]; do |
| 52 | 37 | dir="$(CDPATH= cd -P -- "$(dirname "$src")" >/dev/null && pwd)" |
| 53 | 38 | src="$(readlink "$src")" |
| @@ -58,691 +43,626 @@ _agent_vm_script_dir() { |
| 58 | 43 | done |
| 59 | 44 | (CDPATH= cd -P -- "$(dirname "$src")" >/dev/null && pwd) |
| 60 | 45 | } |
| 61 | | −删除:AGENT_VM_SCRIPT_DIR="$(_agent_vm_script_dir)" |
| 62 | | −删除: |
| 63 | | −删除:# Prompt for a value with a default. Reads from /dev/tty so this still works |
| 64 | | −删除:# when called inside command substitution. Writes the prompt to stderr and the |
| 65 | | −删除:# answer (or the default if the user just pressed Enter) to stdout. |
| 66 | | −删除:_agent_vm_ask() { |
| 67 | | −删除: local prompt="$1" default="$2" reply="" |
| 68 | | −删除: printf ' %s [%s]: ' "$prompt" "$default" >&2 |
| 69 | | −删除: IFS= read -r reply </dev/tty 2>/dev/null || reply="" |
| 70 | | −删除: printf '%s\n' "${reply:-$default}" |
| 71 | | −删除:} |
| 72 | | −删除: |
| 73 | | −删除:# Yes/no prompt. Second arg is the default: Y or N (case-insensitive). Prints |
| 74 | | −删除:# 1 (yes) or 0 (no) to stdout. Empty input picks the default. |
| 75 | | −删除:_agent_vm_ask_yn() { |
| 76 | | −删除: local prompt="$1" default="${2:-Y}" reply="" indicator |
| 77 | | −删除: case "$default" in |
| 78 | | −删除: [Yy]*) indicator="[Y/n]"; default=Y ;; |
| 79 | | −删除: *) indicator="[y/N]"; default=N ;; |
| 80 | | −删除: esac |
| 81 | | −删除: printf ' %s %s: ' "$prompt" "$indicator" >&2 |
| 82 | | −删除: IFS= read -r reply </dev/tty 2>/dev/null || reply="" |
| 83 | | −删除: reply="${reply:-$default}" |
| 84 | | −删除: case "$reply" in |
| 85 | | −删除: [Yy]*) printf '1\n' ;; |
| 86 | | −删除: *) printf '0\n' ;; |
| 87 | | −删除: esac |
| 88 | | −删除:} |
| 89 | | −删除: |
| 90 | | −删除:# Prompt for a positive integer with default. Re-prompts on invalid input. |
| 91 | | −删除:# Used for disk/memory/cpus where a typo (e.g. "10G") would otherwise produce |
| 92 | | −删除:# a cryptic limactl error several seconds later. |
| 93 | | −删除:_agent_vm_ask_int() { |
| 94 | | −删除: local prompt="$1" default="$2" reply |
| 95 | | −删除: while true; do |
| 96 | | −删除: reply=$(_agent_vm_ask "$prompt" "$default") |
| 97 | | −删除: if [[ "$reply" =~ ^[1-9][0-9]*$ ]]; then |
| 98 | | −删除: printf '%s\n' "$reply" |
| 99 | | −删除: return 0 |
| 100 | | −删除: fi |
| 101 | | −删除: printf ' (must be a positive integer, e.g. 10 — got: %s)\n' "$reply" >&2 |
| 102 | | −删除: done |
| 103 | | −删除:} |
| 104 | | −删除: |
| 105 | | −删除:# Validate a positive-integer arg from the CLI (no retry — fail fast). |
| 106 | | −删除:_agent_vm_validate_int() { |
| 107 | | −删除: local name="$1" val="$2" |
| 108 | | −删除: if [[ ! "$val" =~ ^[1-9][0-9]*$ ]]; then |
| 109 | | −删除: echo "Error: $name must be a positive integer (got: '$val')" >&2 |
| 110 | | −删除: return 1 |
| 111 | | −删除: fi |
| 112 | | −删除:} |
| 113 | | −删除: |
| 114 | | −删除:# Check Linux prerequisites Lima needs to spin up a QEMU+KVM VM. macOS uses |
| 115 | | −删除:# different backends (vz/qemu-via-brew) so this is a no-op there. Returns |
| 116 | | −删除:# non-zero with actionable install/permission hints when something's missing — |
| 117 | | −删除:# without this, the user gets a generic `Error: Failed to create base VM` and |
| 118 | | −删除:# has to dig into `~/.lima/<vm>/ha.stderr.log` to figure out why. |
| 119 | | −删除:_agent_vm_check_linux_prereqs() { |
| 120 | | −删除: [[ "$(uname -s)" == "Linux" ]] || return 0 |
| 121 | | −删除: |
| 122 | | −删除: local arch_bin arch_pkg |
| 123 | | −删除: case "$(uname -m)" in |
| 124 | | −删除: x86_64) arch_bin="qemu-system-x86_64"; arch_pkg="qemu-system-x86" ;; |
| 125 | | −删除: aarch64|arm64) arch_bin="qemu-system-aarch64"; arch_pkg="qemu-system-arm" ;; |
| 126 | | −删除: *) arch_bin="qemu-system-$(uname -m)"; arch_pkg="qemu-system" ;; |
| 127 | | −删除: esac |
| 128 | | −删除: |
| 129 | | −删除: local errs=0 |
| 46 | +新增:AGENT_VM_SCRIPT_DIR="$(_agent_vm_script_dir "${BASH_SOURCE[0]:-$0}")" |
| 47 | +新增:if [[ -z "$AGENT_VM_SCRIPT_DIR" ]]; then |
| 48 | +新增: echo "agent-vm: cannot find the directory of agent-vm.sh" >&2 |
| 49 | +新增: return 1 2>/dev/null || exit 1 |
| 50 | +新增:fi |
| 130 | 51 | |
| 131 | | −删除: if ! command -v "$arch_bin" &>/dev/null; then |
| 132 | | −删除: echo "Error: Lima needs '$arch_bin' on PATH (not found)." >&2 |
| 133 | | −删除: echo " Install with: sudo apt-get install $arch_pkg" >&2 |
| 134 | | −删除: errs=1 |
| 52 | +新增:# The rest of agent-vm, one file per concern, sourced like this one. Only |
| 53 | +新增:# functions and settings: nothing runs until a command does. A file that is |
| 54 | +新增:# missing stops here, not half-way through a command. |
| 55 | +新增:for _agent_vm_lib in ui options host vm mounts git runtime env info install doctor help setup; do |
| 56 | +新增: _agent_vm_lib="$AGENT_VM_SCRIPT_DIR/lib/$_agent_vm_lib.sh" |
| 57 | +新增: if [[ ! -r "$_agent_vm_lib" ]] || ! . "$_agent_vm_lib"; then |
| 58 | +新增: echo "agent-vm: cannot load $_agent_vm_lib" >&2 |
| 59 | +新增: unset _agent_vm_lib |
| 60 | +新增: return 1 2>/dev/null || exit 1 |
| 135 | 61 | fi |
| 136 | | −删除: |
| 137 | | −删除: if [[ ! -e /dev/kvm ]]; then |
| 138 | | −删除: echo "Error: /dev/kvm does not exist (KVM unavailable)." >&2 |
| 139 | | −删除: echo " Hardware virtualization may be disabled in BIOS, or the kernel" >&2 |
| 140 | | −删除: echo " lacks KVM support (nested virt in a guest VM, etc.)." >&2 |
| 141 | | −删除: errs=1 |
| 142 | | −删除: elif [[ ! -r /dev/kvm || ! -w /dev/kvm ]]; then |
| 143 | | −删除: echo "Error: /dev/kvm exists but you don't have read/write access." >&2 |
| 144 | | −删除: local groups |
| 145 | | −删除: groups="$(id -nG 2>/dev/null || true)" |
| 146 | | −删除: if [[ " $groups " != *" kvm "* ]]; then |
| 147 | | −删除: echo " Fix: sudo usermod -aG kvm \"\$USER\"" >&2 |
| 148 | | −删除: echo " Then log out and back in (or run 'newgrp kvm') so the new" >&2 |
| 149 | | −删除: echo " group membership takes effect." >&2 |
| 62 | +新增:done |
| 63 | +新增:unset _agent_vm_lib |
| 64 | +新增: |
| 65 | +新增:# --- starting a project's VM -------------------------------------------------- |
| 66 | +新增: |
| 67 | +新增:# The security checks of a start, for the project <dir> and its VM <vm>. Reads the caller's |
| 68 | +新增:# vm_up, scratch and rdonly, and sets its protect_git, ro_names and |
| 69 | +新增:# hooks_notes. With vm_up set (a running VM that keeps running), the risks are |
| 70 | +新增:# printed and nothing is asked. Fails when the start must stop. |
| 71 | +新增:_agent_vm_start_checks() { |
| 72 | +新增: local host_dir="$1" vm_name="$2" lima_st=0 |
| 73 | +新增: protect_git="" ro_names="" hooks_notes="" |
| 74 | +新增: |
| 75 | +新增: # Every .git read-only for the guest, when this Lima can enforce it and it |
| 76 | +新增: # was not turned off. The mounts built afterwards, on every path, follow the |
| 77 | +新增: # answer. A Lima that cannot, with shares left writable and nobody having |
| 78 | +新增: # asked for a writable .git, is a risk to accept. One whose answer could not |
| 79 | +新增: # be read stops the start: taking it for a no would drop the protection of |
| 80 | +新增: # the VM. |
| 81 | +新增: if [[ -n "$scratch" ]]; then |
| 82 | +新增: : |
| 83 | +新增: elif _agent_vm_writable_git_optout; then |
| 84 | +新增: _agent_vm_writable_git_warning >&2 |
| 85 | +新增: else |
| 86 | +新增: _agent_vm_lima_protects_git || lima_st=$? |
| 87 | +新增: if [[ "$lima_st" == 0 ]]; then |
| 88 | +新增: protect_git=1 |
| 89 | +新增: elif [[ -n "$rdonly" ]]; then |
| 90 | +新增: : |
| 91 | +新增: elif [[ "$lima_st" == 2 ]]; then |
| 92 | +新增: echo "Error: cannot tell whether this Lima keeps .git read-only: 'limactl validate' gave no answer agent-vm knows ('agent-vm doctor' shows more)." >&2 |
| 93 | +新增: echo " --readonly, or --unsafe-writable-git to accept a writable .git, still starts the VM." >&2 |
| 94 | +新增: return 1 |
| 95 | +新增: elif _agent_vm_unprotected_mount_is_sshfs "$vm_name"; then |
| 96 | +新增: # Asked below, with the rest of the disk. |
| 97 | +新增: : |
| 98 | +新增: elif [[ -n "$vm_up" ]]; then |
| 99 | +新增: echo "Warning: this Lima cannot keep .git read-only, so the VM can write .git ('agent-vm doctor' says more)." >&2 |
| 150 | 100 | else |
| 151 | | −删除: echo " You're already in the kvm group but /dev/kvm denies access." >&2 |
| 152 | | −删除: echo " Check ownership/mode: ls -l /dev/kvm" >&2 |
| 101 | +新增: _agent_vm_git_protection_hint | _agent_vm_box "Lima cannot keep .git read-only" |
| 102 | +新增: if ! _agent_vm_confirm_unsafe; then |
| 103 | +新增: echo "Aborted. Install that Lima build, or use --readonly; --unsafe-writable-git accepts a writable .git." >&2 |
| 104 | +新增: return 1 |
| 105 | +新增: fi |
| 153 | 106 | fi |
| 154 | | −删除: errs=1 |
| 155 | | −删除: fi |
| 156 | | −删除: |
| 157 | | −删除: [[ $errs -eq 0 ]] |
| 158 | | −删除:} |
| 159 | | −删除: |
| 160 | | −删除:# Lima leaves a partial state dir behind if `limactl create` is interrupted |
| 161 | | −删除:# (Ctrl-C before lima.yaml is written). After that every subsequent limactl |
| 162 | | −删除:# call on that name dies with `open ~/.lima/<vm>/lima.yaml: no such file or |
| 163 | | −删除:# directory` — pre-emptively clean the dir so the next setup/start works. |
| 164 | | −删除:_agent_vm_clean_partial_state() { |
| 165 | | −删除: local vm_name="$1" |
| 166 | | −删除: local lima_dir="$HOME/.lima/$vm_name" |
| 167 | | −删除: if [[ -d "$lima_dir" ]] && [[ ! -f "$lima_dir/lima.yaml" ]]; then |
| 168 | | −删除: echo "Detected partial VM state at $lima_dir (no lima.yaml) — cleaning up." >&2 |
| 169 | | −删除: rm -rf "$lima_dir" |
| 170 | | −删除: fi |
| 171 | | −删除:} |
| 172 | | −删除: |
| 173 | | −删除:# Resolve a user-supplied directory argument to the same absolute form the |
| 174 | | −删除:# VM-running commands use. |
| 175 | | −删除:# |
| 176 | | −删除:# Those commands all derive the name from `$(pwd)`, so they never see a relative |
| 177 | | −删除:# or trailing-slash path. `name` and `info` do take a directory argument, and the |
| 178 | | −删除:# name is a hash of that *string*: without this, `agent-vm name /tmp` and |
| 179 | | −删除:# `agent-vm name /tmp/` return two different VMs for one directory, and neither |
| 180 | | −删除:# need match what `cd /tmp && agent-vm opencode` produces. |
| 181 | | −删除:# |
| 182 | | −删除:# Logical pwd (no `-P`), to agree with the `$(pwd)` the other commands use. |
| 183 | | −删除:# A directory that does not exist is rejected rather than hashed: a name derived |
| 184 | | −删除:# from an unresolvable path is wrong in a way nothing downstream would catch. |
| 185 | | −删除:# |
| 186 | | −删除:# `CDPATH=` is not cosmetic. With CDPATH set in the environment, `cd <relative>` |
| 187 | | −删除:# searches it *before* the current directory and prints where it landed — so |
| 188 | | −删除:# this would both emit a stray line into the captured value and resolve a |
| 189 | | −删除:# DIFFERENT directory than the `-d` test above just validated. Clearing it keeps |
| 190 | | −删除:# the argument meaning "relative to cwd", like every other path-taking tool. |
| 191 | | −删除:_agent_vm_abs_dir() { |
| 192 | | −删除: local dir="${1:-$(pwd)}" |
| 193 | | −删除: if [[ ! -d "$dir" ]]; then |
| 194 | | −删除: echo "Error: no such directory: $dir" >&2 |
| 195 | | −删除: return 1 |
| 196 | | −删除: fi |
| 197 | | −删除: (CDPATH= cd -- "$dir" >/dev/null && pwd) |
| 198 | | −删除:} |
| 199 | | −删除: |
| 200 | | −删除:# Generate a deterministic VM name for a directory |
| 201 | | −删除:_agent_vm_name() { |
| 202 | | −删除: local dir="${1:-$(pwd)}" |
| 203 | | −删除: local hash |
| 204 | | −删除: hash=$(echo -n "$dir" | shasum -a 256 | cut -c1-8) |
| 205 | | −删除: local base |
| 206 | | −删除: base=$(basename "$dir" | tr -cs 'a-zA-Z0-9' '-' | sed 's/^-//;s/-$//') |
| 207 | | −删除: echo "agent-vm-${base}-${hash}" |
| 208 | | −删除:} |
| 209 | | −删除: |
| 210 | | −删除:# Exact-line match against an already-captured string, with no pipe. |
| 211 | | −删除:# |
| 212 | | −删除:# `cmd | grep -q needle` is unsafe for anyone who sources this file from a |
| 213 | | −删除:# script running under `set -o pipefail`: grep -q closes the pipe as soon as it |
| 214 | | −删除:# matches, limactl takes a SIGPIPE and exits 141, and pipefail fails the whole |
| 215 | | −删除:# pipeline. The result is an intermittent false negative that depends on how |
| 216 | | −删除:# much limactl still had to write — it looks like "the VM doesn't exist". So |
| 217 | | −删除:# capture the output first, then match it in the shell. |
| 218 | | −删除:_agent_vm_has_line() { |
| 219 | | −删除: case $'\n'"$1"$'\n' in |
| 220 | | −删除: *$'\n'"$2"$'\n'*) return 0 ;; |
| 221 | | −删除: *) return 1 ;; |
| 222 | | −删除: esac |
| 223 | | −删除:} |
| 224 | | −删除: |
| 225 | | −删除:# Check if a VM exists (any state). |
| 226 | | −删除:# 0 = yes · 1 = no · 2 = could not ask (limactl itself failed). |
| 227 | | −删除:# The third status matters: an empty answer from a failed query is otherwise |
| 228 | | −删除:# indistinguishable from "no such VM", and `info` would report a confident 0. |
| 229 | | −删除:_agent_vm_exists() { |
| 230 | | −删除: local list |
| 231 | | −删除: list="$(limactl list -q 2>/dev/null)" || return 2 |
| 232 | | −删除: _agent_vm_has_line "$list" "$1" |
| 233 | | −删除:} |
| 234 | | −删除: |
| 235 | | −删除:# Check if a VM is running. Same three statuses as _agent_vm_exists. |
| 236 | | −删除:_agent_vm_running() { |
| 237 | | −删除: local list |
| 238 | | −删除: list="$(limactl list --format '{{.Name}} {{.Status}}' 2>/dev/null)" || return 2 |
| 239 | | −删除: _agent_vm_has_line "$list" "$1 Running" |
| 240 | | −删除:} |
| 241 | | −删除: |
| 242 | | −删除:# Check if the base VM template exists. Kept as a named helper so integrators |
| 243 | | −删除:# don't have to hardcode the template name to answer "do I need to run setup?". |
| 244 | | −删除:# Same three statuses as _agent_vm_exists. |
| 245 | | −删除:_agent_vm_base_exists() { |
| 246 | | −删除: _agent_vm_exists "$AGENT_VM_TEMPLATE" |
| 247 | | −删除:} |
| 248 | | −删除: |
| 249 | | −删除:# Turn one of those exit statuses into the value `info` publishes. |
| 250 | | −删除:_agent_vm_tristate() { |
| 251 | | −删除: case "$1" in |
| 252 | | −删除: 0) echo 1 ;; |
| 253 | | −删除: 1) echo 0 ;; |
| 254 | | −删除: *) echo unknown ;; |
| 255 | | −删除: esac |
| 256 | | −删除:} |
| 257 | | −删除: |
| 258 | | −删除:# Was <vm_name> cloned from an older base than the current one? |
| 259 | | −删除:# Prints 1 (stale), 0 (up to date), or "unknown" when there is nothing recorded |
| 260 | | −删除:# to compare against — never guess from a missing file. A VM with no version |
| 261 | | −删除:# marker but a known base predates the marker, which makes it stale. |
| 262 | | −删除:_agent_vm_stale_state() { |
| 263 | | −删除: local vm_name="$1" |
| 264 | | −删除: local base_ver="$AGENT_VM_STATE_DIR/.agent-vm-base-version" |
| 265 | | −删除: local vm_ver="$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 266 | | −删除: if [[ ! -f "$base_ver" ]]; then |
| 267 | | −删除: echo unknown |
| 268 | | −删除: elif [[ ! -f "$vm_ver" ]]; then |
| 269 | | −删除: echo 1 |
| 270 | | −删除: elif [[ "$(cat "$base_ver" 2>/dev/null)" != "$(cat "$vm_ver" 2>/dev/null)" ]]; then |
| 271 | | −删除: echo 1 |
| 272 | | −删除: else |
| 273 | | −删除: echo 0 |
| 274 | 107 | fi |
| 275 | | −删除:} |
| 276 | 108 | |
| 277 | | −删除:# Stage a single host file at <dst> via hardlink, falling back to copy if the |
| 278 | | −删除:# source and destination live on different filesystems. Hardlinking keeps the |
| 279 | | −删除:# content live-synced with the host (same inode) without exposing the source's |
| 280 | | −删除:# parent directory to the VM. The copy fallback preserves the no-exposure |
| 281 | | −删除:# property but loses live sync until the next VM (re)start. |
| 282 | | −删除:_agent_vm_stage_file() { |
| 283 | | −删除: local src="$1" dst="$2" |
| 284 | | −删除: mkdir -p "$(dirname "$dst")" 2>/dev/null || return 1 |
| 285 | | −删除: rm -f "$dst" |
| 286 | | −删除: if ln "$src" "$dst" 2>/dev/null; then |
| 287 | | −删除: return 0 |
| 288 | | −删除: fi |
| 289 | | −删除: if cp -p "$src" "$dst" 2>/dev/null; then |
| 290 | | −删除: echo "Warning: Staged '${src}' via copy (cross-filesystem hardlink failed); live host changes will not propagate until VM (re)start." >&2 |
| 291 | | −删除: return 0 |
| 109 | +新增: # Not only .git: shares Lima does not confine leave the rest of the disk |
| 110 | +新增: # in reach (see _agent_vm_unprotected_mount_is_sshfs), --unsafe-writable-git |
| 111 | +新增: # or not. --readonly cannot be enforced on them either: refused before |
| 112 | +新增: # anything boots. |
| 113 | +新增: if [[ -z "$scratch" && -z "$protect_git" ]] && _agent_vm_unprotected_mount_is_sshfs "$vm_name"; then |
| 114 | +新增: if [[ -n "$rdonly" ]]; then |
| 115 | +新增: echo "Error: --readonly cannot be enforced here: the shares would be reverse-sshfs, which this Lima does not confine. Install the Lima build 'agent-vm setup' offers, or use --scratch." >&2 |
| 116 | +新增: return 1 |
| 117 | +新增: elif [[ -n "$vm_up" ]]; then |
| 118 | +新增: echo "Warning: this Lima cannot keep the VM to its shares: root in the VM may reach files outside them, your SSH keys included ('agent-vm doctor' says more)." >&2 |
| 119 | +新增: else |
| 120 | +新增: { _agent_vm_sshfs_exposure_note; _agent_vm_git_protection_hint; } | _agent_vm_box "Lima cannot keep the VM to its shares" |
| 121 | +新增: if ! _agent_vm_confirm_unsafe; then |
| 122 | +新增: echo "Aborted. Install that Lima build, or use --scratch, which shares nothing." >&2 |
| 123 | +新增: return 1 |
| 124 | +新增: fi |
| 125 | +新增: fi |
| 292 | 126 | fi |
| 293 | | −删除: return 1 |
| 294 | | −删除:} |
| 295 | 127 | |
| 296 | | −删除:# Remove all per-VM state files (version marker, terminfo cache, file mount |
| 297 | | −删除:# cache, staging dirs). Called after a VM is deleted or before it is re-cloned |
| 298 | | −删除:# via --reset. |
| 299 | | −删除:_agent_vm_cleanup_state() { |
| 300 | | −删除: local vm_name="$1" |
| 301 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 302 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}" |
| 303 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}" |
| 304 | | −删除: rm -rf "$AGENT_VM_STATE_DIR/file-mounts/${vm_name}" |
| 305 | | −删除:} |
| 306 | | −删除: |
| 307 | | −删除:# Build the .mounts JSON array for a VM. The first entry is always the project |
| 308 | | −删除:# dir (writable). Additional entries come from ~/.agent-vm/volumes, parsed as |
| 309 | | −删除:# Docker-Compose-ish `source[:destination][:mode]` (mode ∈ {ro,rw}, default ro). |
| 310 | | −删除:# |
| 311 | | −删除:# Side effects: stages any file mounts as hardlinks under |
| 312 | | −删除:# ~/.agent-vm/file-mounts/<vm>/ and persists the file mount metadata to |
| 313 | | −删除:# ~/.agent-vm/.agent-vm-file-mounts-<vm> so subsequent starts can re-apply the |
| 314 | | −删除:# inside-VM bind mounts without re-parsing the volumes file. Stdout: the |
| 315 | | −删除:# mounts JSON array (consumed by `limactl edit --set ".mounts = ..."`). |
| 316 | | −删除:_agent_vm_build_mounts_json() { |
| 317 | | −删除: local vm_name="$1" host_dir="$2" |
| 318 | | −删除: local mounts_json="[{\"location\": \"${host_dir}\", \"writable\": true}" |
| 319 | | −删除: local mounts_file="$AGENT_VM_STATE_DIR/volumes" |
| 320 | | −删除: local file_mount_entries=() |
| 321 | | −删除: local file_mounts_cache="$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}" |
| 322 | | −删除: |
| 323 | | −删除: if [[ -f "$mounts_file" ]]; then |
| 324 | | −删除: local staging_idx=0 |
| 325 | | −删除: while IFS= read -r line || [[ -n "$line" ]]; do |
| 326 | | −删除: line="${line%%#*}" # strip comments |
| 327 | | −删除: line="${line#"${line%%[![:space:]]*}"}" # trim leading whitespace |
| 328 | | −删除: line="${line%"${line##*[![:space:]]}"}" # trim trailing whitespace |
| 329 | | −删除: [[ -z "$line" ]] && continue |
| 330 | | −删除: # Parse source[:destination][:mode] syntax (like docker compose volumes). |
| 331 | | −删除: # The trailing mode segment is only recognized when it equals "ro" or |
| 332 | | −删除: # "rw" — anything else is treated as a destination path. |
| 333 | | −删除: local src="$line" dst="" mode="ro" |
| 334 | | −删除: if [[ "$line" == *:ro || "$line" == *:rw ]]; then |
| 335 | | −删除: mode="${line##*:}" |
| 336 | | −删除: line="${line%:*}" |
| 337 | | −删除: fi |
| 338 | | −删除: if [[ "$line" == *:* ]]; then |
| 339 | | −删除: src="${line%%:*}" |
| 340 | | −删除: dst="${line#*:}" |
| 341 | | −删除: else |
| 342 | | −删除: src="$line" |
| 343 | | −删除: fi |
| 344 | | −删除: src="${src/#\~/$HOME}" # expand ~ |
| 345 | | −删除: # Reject characters that would break JSON interpolation below or the |
| 346 | | −删除: # pipe-separated cache format used for file mounts. |
| 347 | | −删除: if [[ "$src" == *[$'"\\\n|']* || "$dst" == *[$'"\\\n|']* ]]; then |
| 348 | | −删除: echo "Warning: Mount entry '${line}' (from ~/.agent-vm/volumes) contains invalid characters (quote/backslash/newline/pipe), skipping." >&2 |
| 128 | +新增: # With it, the names the shares need: those always listed, and one per |
| 129 | +新增: # hooks folder git runs from in the project (see _agent_vm_project_hooks), |
| 130 | +新增: # its first component. A dot-name (.husky, .githooks) is taken as is; any |
| 131 | +新增: # other is read-only in every folder of the project, so it is asked first |
| 132 | +新增: # (yes by default, and when no one can answer). A folder that cannot be |
| 133 | +新增: # protected, or one declined, is a risk to accept. So are git config files |
| 134 | +新增: # in the project, and settings running commands from it. Under --readonly |
| 135 | +新增: # the VM writes none of them: the names are taken as they come, with no |
| 136 | +新增: # warning and no question. |
| 137 | +新增: local hooks_rel hooks_in hooks_name hooks_names=() |
| 138 | +新增: if [[ -n "$protect_git" ]]; then |
| 139 | +新增: while IFS=$'\t' read -r hooks_rel hooks_in; do |
| 140 | +新增: [[ -n "$hooks_rel" ]] || continue |
| 141 | +新增: if [[ -n "$rdonly" ]]; then |
| 142 | +新增: hooks_name="$(_agent_vm_hooks_name "$hooks_in")" && hooks_names+=("$hooks_name") |
| 349 | 143 | continue |
| 350 | 144 | fi |
| 351 | | −删除: if [[ ! -e "$src" ]]; then |
| 352 | | −删除: echo "Warning: Mount path '${src}' (from ~/.agent-vm/volumes) does not exist, skipping." >&2 |
| 353 | | −删除: continue |
| 354 | | −删除: fi |
| 355 | | −删除: if [[ -f "$src" ]]; then |
| 356 | | −删除: if [[ "$mode" == "rw" ]]; then |
| 357 | | −删除: echo "Warning: Mount entry '${line}' (from ~/.agent-vm/volumes) requests rw on a file; only directories support rw. Mount the parent directory instead. Skipping." >&2 |
| 358 | | −删除: continue |
| 145 | +新增: if ! hooks_name="$(_agent_vm_hooks_name "$hooks_in")"; then |
| 146 | +新增: if [[ "$hooks_rel" == . ]]; then |
| 147 | +新增: echo "Warning: git's core.hooksPath is the project directory itself, which the VM can write: git on this machine runs the hooks the agent puts there." >&2 |
| 148 | +新增: else |
| 149 | +新增: echo "Warning: git's core.hooksPath is '$hooks_rel', in the project, and its name cannot be made read-only: git on this machine runs the hooks the agent puts there." >&2 |
| 359 | 150 | fi |
| 360 | | −删除: # File mount: hardlink the source into a per-VM host staging dir so |
| 361 | | −删除: # the VM sees only this file (never the source's parent). Lima mounts |
| 362 | | −删除: # the staging dir via virtiofs; a bind mount inside the VM (applied |
| 363 | | −删除: # after boot) exposes the file at its final destination. |
| 364 | | −删除: local filename |
| 365 | | −删除: filename="$(basename "$src")" |
| 366 | | −删除: local file_staging_dir="$AGENT_VM_STATE_DIR/file-mounts/${vm_name}/${staging_idx}" |
| 367 | | −删除: local host_staging="${file_staging_dir}/${filename}" |
| 368 | | −删除: if ! _agent_vm_stage_file "$src" "$host_staging"; then |
| 369 | | −删除: echo "Warning: Failed to stage '${src}', skipping." >&2 |
| 370 | | −删除: rm -rf "$file_staging_dir" |
| 371 | | −删除: continue |
| 151 | +新增: if [[ -z "$vm_up" ]] && ! _agent_vm_confirm_unsafe; then |
| 152 | +新增: echo "Aborted. Point core.hooksPath to a folder of the project (agent-vm keeps it read-only), or outside it." >&2 |
| 153 | +新增: return 1 |
| 372 | 154 | fi |
| 373 | | −删除: local staging_mount="/tmp/.agent-vm-file-mounts/${staging_idx}" |
| 374 | | −删除: local bind_dst="${dst:-${src}}" |
| 375 | | −删除: file_mount_entries+=("${src}|${host_staging}|${staging_mount}/${filename}|${bind_dst}") |
| 376 | | −删除: mounts_json+=", {\"location\": \"${file_staging_dir}\", \"mountPoint\": \"${staging_mount}\", \"writable\": false}" |
| 377 | | −删除: staging_idx=$((staging_idx + 1)) |
| 378 | 155 | continue |
| 379 | 156 | fi |
| 380 | | −删除: if [[ ! -d "$src" ]]; then |
| 381 | | −删除: echo "Warning: Mount path '${src}' (from ~/.agent-vm/volumes) is not a regular file or directory, skipping." >&2 |
| 157 | +新增: if [[ "$hooks_name" != .* && -z "$vm_up" ]] && ! _agent_vm_prompts_disabled_by >/dev/null \ |
| 158 | +新增: && _agent_vm_can_ask \ |
| 159 | +新增: && [[ "$(_agent_vm_ask_yn "Git runs hooks from $hooks_rel. Make every '$hooks_name' folder in the project read-only for the VM?" Y)" != "1" ]]; then |
| 160 | +新增: echo "Warning: '$hooks_name' stays writable, and git on this machine runs the hooks the agent puts in $hooks_rel." >&2 |
| 161 | +新增: if ! _agent_vm_confirm_unsafe; then |
| 162 | +新增: echo "Aborted. Answer yes to protect it, or point core.hooksPath outside the project." >&2 |
| 163 | +新增: return 1 |
| 164 | +新增: fi |
| 382 | 165 | continue |
| 383 | 166 | fi |
| 384 | | −删除: local writable="false" |
| 385 | | −删除: [[ "$mode" == "rw" ]] && writable="true" |
| 386 | | −删除: if [[ -n "$dst" ]]; then |
| 387 | | −删除: mounts_json+=", {\"location\": \"${src}\", \"mountPoint\": \"${dst}\", \"writable\": ${writable}}" |
| 388 | | −删除: else |
| 389 | | −删除: mounts_json+=", {\"location\": \"${src}\", \"writable\": ${writable}}" |
| 167 | +新增: hooks_names+=("$hooks_name") |
| 168 | +新增: hooks_notes="$hooks_notes$(_agent_vm_hooks_note "$hooks_rel" "$hooks_name")"$'\n' |
| 169 | +新增: done <<< "$(_agent_vm_project_hooks "$host_dir")" |
| 170 | +新增: ro_names="$(_agent_vm_names_json ${hooks_names[@]+"${hooks_names[@]}"})" |
| 171 | +新增: |
| 172 | +新增: local git_risks="" |
| 173 | +新增: [[ -n "$rdonly" ]] || git_risks="$(_agent_vm_project_config_risks "$host_dir")" |
| 174 | +新增: if [[ -n "$git_risks" ]]; then |
| 175 | +新增: echo "Warning: git on this machine uses these, and the VM can write them:" >&2 |
| 176 | +新增: printf '%s\n' "$git_risks" | sed 's/^/ /' >&2 |
| 177 | +新增: if [[ -z "$vm_up" ]] && ! _agent_vm_confirm_unsafe; then |
| 178 | +新增: echo "Aborted. Move them out of the project, or have them name commands outside it." >&2 |
| 179 | +新增: return 1 |
| 390 | 180 | fi |
| 391 | | −删除: done < "$mounts_file" |
| 181 | +新增: fi |
| 392 | 182 | fi |
| 393 | | −删除: mounts_json+="]" |
| 394 | | −删除: |
| 395 | | −删除: rm -f "$file_mounts_cache" |
| 396 | | −删除: if [[ ${#file_mount_entries[@]} -gt 0 ]]; then |
| 397 | | −删除: printf '%s\n' "${file_mount_entries[@]}" > "$file_mounts_cache" |
| 183 | +新增: # A repository not named .git, which no name protects (see |
| 184 | +新增: # _agent_vm_bare_repo_state): only a VM that can write needs the setting. |
| 185 | +新增: if [[ -z "$rdonly" && -z "$scratch" ]]; then |
| 186 | +新增: _agent_vm_check_bare_repo_setting "$vm_up" || return 1 |
| 398 | 187 | fi |
| 399 | | −删除: |
| 400 | | −删除: printf '%s' "$mounts_json" |
| 401 | 188 | } |
| 402 | 189 | |
| 403 | | −删除:# Current resources of <vm_name> as "cpus|memory_gib|disk_gib". |
| 404 | | −删除:# Prints nothing (and returns 1) when the VM is unknown to Lima. No pipe into |
| 405 | | −删除:# grep/head: see _agent_vm_has_line for why that is unsafe under pipefail. |
| 406 | | −删除:_agent_vm_resources() { |
| 407 | | −删除: local vm_name="$1" all line |
| 408 | | −删除: all="$(limactl list --format '{{.Name}}|{{.CPUs}}|{{.Memory}}|{{.Disk}}' 2>/dev/null || true)" |
| 409 | | −删除: while IFS= read -r line; do |
| 410 | | −删除: case "$line" in |
| 411 | | −删除: "${vm_name}|"*) |
| 412 | | −删除: local cpus mem_bytes disk_bytes |
| 413 | | −删除: IFS='|' read -r _ cpus mem_bytes disk_bytes <<< "$line" |
| 414 | | −删除: printf '%s|%s|%s\n' "$cpus" "$((mem_bytes / 1073741824))" "$((disk_bytes / 1073741824))" |
| 415 | | −删除: return 0 ;; |
| 416 | | −删除: esac |
| 417 | | −删除: done <<< "$all" |
| 418 | | −删除: return 1 |
| 190 | +新增:# A VM that ran when the start began, so whose start asked nothing (see |
| 191 | +新增:# _agent_vm_start_checks), has just been stopped to boot again: ask now, as |
| 192 | +新增:# for any VM about to boot. Reads and clears the caller's vm_up. Fails, with |
| 193 | +新增:# the VM left stopped, when the start must stop. |
| 194 | +新增:_agent_vm_recheck_after_stop() { |
| 195 | +新增: [[ -n "$vm_up" ]] || return 0 |
| 196 | +新增: vm_up="" |
| 197 | +新增: if ! _agent_vm_start_checks "$1" "$2"; then |
| 198 | +新增: echo "VM '$2' is stopped." >&2 |
| 199 | +新增: return 1 |
| 200 | +新增: fi |
| 419 | 201 | } |
| 420 | 202 | |
| 421 | | −删除:# Print VM resource details (CPUs, memory, disk) |
| 422 | | −删除:_agent_vm_print_resources() { |
| 423 | | −删除: local res cpus mem_gib disk_gib |
| 424 | | −删除: if res="$(_agent_vm_resources "$1")"; then |
| 425 | | −删除: IFS='|' read -r cpus mem_gib disk_gib <<< "$res" |
| 426 | | −删除: echo " Resources: CPUs: ${cpus}, Memory: ${mem_gib} GiB, Disk: ${disk_gib} GiB" |
| 203 | +新增:# Stops <vm>, for the project <dir>, to boot it again changed, then asks what |
| 204 | +新增:# its start did not (see above). Fails, saying so, when it did not stop. |
| 205 | +新增:_agent_vm_stop_to_change() { |
| 206 | +新增: if ! _agent_vm_stop_vm "$1"; then |
| 207 | +新增: echo "Error: could not stop VM '$1': nothing was changed." >&2 |
| 208 | +新增: return 1 |
| 427 | 209 | fi |
| 210 | +新增: _agent_vm_recheck_after_stop "$2" "$1" |
| 428 | 211 | } |
| 429 | 212 | |
| 430 | | −删除:# Would the requested resources actually change anything on <vm_name>? |
| 431 | | −删除:# Empty request fields mean "not specified". Returns 0 when something differs |
| 432 | | −删除:# (or when the current values can't be read — never claim "no change" from |
| 433 | | −删除:# missing information), 1 when the VM already matches the request. |
| 434 | | −删除:# |
| 435 | | −删除:# Disk is compared one-way on purpose: Lima can grow a disk but not shrink it, |
| 436 | | −删除:# so a request below the current size is not a change that stopping could apply. |
| 437 | | −删除:# |
| 438 | | −删除:# Both sides are integer GiB, so a VM whose memory is not a whole number of GiB |
| 439 | | −删除:# can still compare unequal every time. That is the pre-existing behaviour |
| 440 | | −删除:# (prompt on every call), not a new failure mode. |
| 441 | | −删除:_agent_vm_resources_differ() { |
| 442 | | −删除: local vm_name="$1" want_cpus="$2" want_mem="$3" want_disk="$4" |
| 443 | | −删除: local cur cur_cpus cur_mem cur_disk |
| 444 | | −删除: if ! cur="$(_agent_vm_resources "$vm_name")"; then |
| 445 | | −删除: return 0 |
| 446 | | −删除: fi |
| 447 | | −删除: IFS='|' read -r cur_cpus cur_mem cur_disk <<< "$cur" |
| 448 | | −删除: if [[ -n "$want_cpus" && "$want_cpus" != "$cur_cpus" ]]; then |
| 449 | | −删除: return 0 |
| 213 | +新增:# Gives the stopped <vm> its shares for the project <dir>, writable or not |
| 214 | +新增:# (<writable> true/false), with the caller's protect_git and ro_names, or none |
| 215 | +新增:# for its scratch, and records them (see _agent_vm_record_mounts). |
| 216 | +新增:_agent_vm_apply_shares() { |
| 217 | +新增: local vm="$1" dir="$2" writable="$3" json="[]" out |
| 218 | +新增: [[ -n "$scratch" ]] || json="$(_agent_vm_build_mounts_json "$vm" "$dir" "$writable" "${protect_git:+$ro_names}")" |
| 219 | +新增: if ! out=$(cd /tmp && limactl edit "$vm" --set "$(_agent_vm_mounts_expr "$json" "$protect_git")" 2>&1); then |
| 220 | +新增: echo "Error: could not set the shares of VM '$vm':" >&2 |
| 221 | +新增: echo "$out" >&2 |
| 222 | +新增: return 1 |
| 450 | 223 | fi |
| 451 | | −删除: if [[ -n "$want_mem" && "$want_mem" != "$cur_mem" ]]; then |
| 452 | | −删除: return 0 |
| 224 | +新增: _agent_vm_record_mounts "$vm" "$json" |
| 225 | +新增:} |
| 226 | +新增: |
| 227 | +新增:# The caller's memory, cpus, ssh_port and disk, on the stopped <vm>. The disk |
| 228 | +新增:# apart, with a warning only: Lima rejects a whole edit that would shrink it. |
| 229 | +新增:_agent_vm_apply_resources() { |
| 230 | +新增: local vm="$1" out args=() |
| 231 | +新增: [[ -z "$memory" ]] || args+=(--memory "$memory") |
| 232 | +新增: [[ -z "$cpus" ]] || args+=(--cpus "$cpus") |
| 233 | +新增: [[ -z "$ssh_port" ]] || args+=(--set ".ssh.localPort = $ssh_port") |
| 234 | +新增: # Never without arguments: `limactl edit <vm>` alone opens $EDITOR. |
| 235 | +新增: if [[ ${#args[@]} -gt 0 ]] && ! out=$(cd /tmp && limactl edit "$vm" "${args[@]}" 2>&1); then |
| 236 | +新增: echo "Error: could not set the memory, CPUs or SSH port of VM '$vm':" >&2 |
| 237 | +新增: echo "$out" >&2 |
| 238 | +新增: return 1 |
| 453 | 239 | fi |
| 454 | | −删除: if [[ -n "$want_disk" && "$want_disk" -gt "$cur_disk" ]]; then |
| 455 | | −删除: return 0 |
| 240 | +新增: if [[ -n "$disk" ]] && ! out=$(cd /tmp && limactl edit "$vm" --disk "$disk" 2>&1); then |
| 241 | +新增: echo "Warning: cannot set the disk of VM '$vm' to $disk GiB: it can grow, not shrink ('agent-vm setup --disk $disk' for a smaller base)." >&2 |
| 242 | +新增: echo "$out" >&2 |
| 456 | 243 | fi |
| 457 | | −删除: return 1 |
| 244 | +新增: return 0 |
| 245 | +新增:} |
| 246 | +新增: |
| 247 | +新增:# The env push and the write probe (see _agent_vm_push_env_and_probe), with |
| 248 | +新增:# the caller's vm_name, host_dir, env_payload, guest_env and guest_runtime, |
| 249 | +新增:# into its probe_out and is_writable. |
| 250 | +新增:_agent_vm_push_and_probe() { |
| 251 | +新增: is_writable="false" |
| 252 | +新增: probe_out="$(_agent_vm_push_env_and_probe "$vm_name" "$host_dir" "$env_payload" \ |
| 253 | +新增: "$guest_env" "$guest_runtime")" && is_writable="true" |
| 254 | +新增: [[ "$probe_out" == *env-ok* ]] || echo "Warning: failed to push the env files into VM '$vm_name'." >&2 |
| 458 | 255 | } |
| 459 | 256 | |
| 460 | 257 | # Ensure the VM for cwd exists and is running, creating/starting as needed |
| 461 | | −删除:# Usage: _agent_vm_ensure_running <vm_name> <host_dir> [--disk GB] [--memory GB] [--reset] |
| 258 | +新增:# Usage: _agent_vm_ensure_running <vm_name> <host_dir> [vm_opts...], the options |
| 259 | +新增:# as _agent_vm_take_opt collects them (checked already). |
| 462 | 260 | _agent_vm_ensure_running() { |
| 463 | 261 | local vm_name="$1" |
| 464 | 262 | local host_dir="$2" |
| 465 | 263 | shift 2 |
| 466 | | −删除: local disk="" memory="" cpus="" reset="" offline="" rdonly="" git_ro="" |
| 264 | +新增: local disk="" memory="" cpus="" ssh_port="" reset="" rdonly="" scratch="" _agent_vm_unsafe_git_flag="" _agent_vm_unsafe_no_prompts="" |
| 467 | 265 | while [[ $# -gt 0 ]]; do |
| 468 | 266 | case "$1" in |
| 469 | 267 | --disk) disk="$2"; shift 2 ;; |
| 470 | | −删除: --memory|--ram) memory="$2"; shift 2 ;; |
| 268 | +新增: --memory) memory="$2"; shift 2 ;; |
| 471 | 269 | --cpus) cpus="$2"; shift 2 ;; |
| 270 | +新增: --ssh-port) ssh_port="$2"; shift 2 ;; |
| 472 | 271 | --reset) reset=1; shift ;; |
| 473 | | −删除: --offline) offline=1; shift ;; |
| 474 | 272 | --readonly) rdonly=1; shift ;; |
| 475 | | −删除: --git-read-only|--git-ro) git_ro=1; shift ;; |
| 273 | +新增: --scratch) scratch=1; shift ;; |
| 274 | +新增: --unsafe-writable-git) _agent_vm_unsafe_git_flag=1; shift ;; |
| 275 | +新增: --unsafe-disable-security-prompts) _agent_vm_unsafe_no_prompts=1; shift ;; |
| 476 | 276 | *) shift ;; |
| 477 | 277 | esac |
| 478 | 278 | done |
| 479 | 279 | |
| 480 | | −删除: # Lima's host mount cannot share a path containing whitespace: the mount |
| 481 | | −删除: # fails silently and the VM starts with a bare, root-owned mountpoint, so |
| 482 | | −删除: # every write into the project (e.g. creating .claude) fails with |
| 483 | | −删除: # "Permission denied". Fail fast with an actionable message instead. |
| 484 | | −删除: if [[ "$host_dir" == *[[:space:]]* ]]; then |
| 485 | | −删除: echo "Error: project path contains whitespace, which Lima cannot mount:" >&2 |
| 280 | +新增: # --scratch (see _agent_vm_run_scratch) shares nothing: no share check |
| 281 | +新增: # below applies to it. |
| 282 | +新增: if [[ -n "$scratch" && ( -n "$reset" || -n "$rdonly" ) ]]; then |
| 283 | +新增: echo "Error: --scratch makes a new VM that shares nothing: --reset and --readonly do not go with it." >&2 |
| 284 | +新增: return 1 |
| 285 | +新增: fi |
| 286 | +新增: |
| 287 | +新增: local want_writable="true" |
| 288 | +新增: [[ -n "$rdonly" ]] && want_writable="false" |
| 289 | +新增: |
| 290 | +新增: # Clamped once, so every path below compares and applies the same values. |
| 291 | +新增: cpus="$(_agent_vm_cap_resource cpus "$cpus")" |
| 292 | +新增: memory="$(_agent_vm_cap_resource memory "$memory")" |
| 293 | +新增: _agent_vm_warn_disk_space "$disk" |
| 294 | +新增: |
| 295 | +新增: # Two VMs set to one port: the second would fail to start. |
| 296 | +新增: if [[ -n "$ssh_port" && "$ssh_port" != 0 ]]; then |
| 297 | +新增: local other |
| 298 | +新增: other="$(limactl list --format '{{.Name}} {{.Config.SSH.LocalPort}}' 2>/dev/null \ |
| 299 | +新增: | awk -v vm="$vm_name" -v p="$ssh_port" '$1 != vm && $2 == p { print $1; exit }')" |
| 300 | +新增: if [[ -n "$other" ]]; then |
| 301 | +新增: echo "Error: SSH port $ssh_port is already set for VM '$other'." >&2 |
| 302 | +新增: return 1 |
| 303 | +新增: fi |
| 304 | +新增: fi |
| 305 | +新增: |
| 306 | +新增: # `cd ~ && agent-vm shell` would hand the VM your dotfiles and SSH keys, |
| 307 | +新增: # read-write, and a share holding agent-vm's own files lets the VM change |
| 308 | +新增: # what the host runs next. |
| 309 | +新增: local unsafe |
| 310 | +新增: if [[ -z "$scratch" ]] && unsafe="$(_agent_vm_unsafe_project "$host_dir")"; then |
| 311 | +新增: echo "Error: refusing to share $host_dir with a VM: it $unsafe." >&2 |
| 312 | +新增: echo "Run agent-vm from a project directory." >&2 |
| 313 | +新增: return 1 |
| 314 | +新增: fi |
| 315 | +新增: |
| 316 | +新增: local bad |
| 317 | +新增: if [[ -z "$scratch" ]] && bad="$(_agent_vm_unmountable_path "$host_dir")"; then |
| 318 | +新增: echo "Error: project path $bad:" >&2 |
| 486 | 319 | echo " $host_dir" >&2 |
| 487 | | −删除: echo "Rename the directory to remove spaces (e.g. with '-'), then retry." >&2 |
| 320 | +新增: echo "Rename the directory, then retry." >&2 |
| 488 | 321 | return 1 |
| 489 | 322 | fi |
| 490 | 323 | |
| 491 | | −删除: # Recover from an interrupted previous run that left ~/.lima/<vm>/ without |
| 492 | | −删除: # a lima.yaml — otherwise every limactl call on this name aborts with a |
| 493 | | −删除: # cryptic "no such file or directory". |
| 324 | +新增: # A run interrupted while Lima created it leaves a folder without lima.yaml. |
| 494 | 325 | _agent_vm_clean_partial_state "$vm_name" |
| 495 | 326 | |
| 496 | 327 | if ! _agent_vm_base_exists; then |
| 497 | | −删除: echo "Error: Base VM not found. Run 'agent-vm setup' first." >&2 |
| 328 | +新增: # Distinguish "never set up" from "setup died halfway": the second one |
| 329 | +新增: # leaves a template that looks fine in `limactl list` but is empty. |
| 330 | +新增: if _agent_vm_exists "$AGENT_VM_TEMPLATE"; then |
| 331 | +新增: echo "Error: Base VM setup did not complete. Run 'agent-vm setup' again." >&2 |
| 332 | +新增: else |
| 333 | +新增: echo "Error: Base VM not found. Run 'agent-vm setup' first." >&2 |
| 334 | +新增: fi |
| 498 | 335 | return 1 |
| 499 | 336 | fi |
| 500 | 337 | |
| 338 | +新增: # The security checks, before anything changes. A running VM is asked |
| 339 | +新增: # nothing until stopped (see _agent_vm_recheck_after_stop). |
| 340 | +新增: local vm_up="" protect_git="" ro_names="" hooks_notes="" |
| 341 | +新增: [[ -z "$reset" ]] && _agent_vm_running "$vm_name" && vm_up=1 |
| 342 | +新增: _agent_vm_start_checks "$host_dir" "$vm_name" || return 1 |
| 343 | +新增: |
| 501 | 344 | # Destroy existing VM if --reset was requested |
| 502 | 345 | if [[ -n "$reset" ]] && _agent_vm_exists "$vm_name"; then |
| 503 | 346 | echo "Resetting VM '$vm_name'..." |
| 504 | | −删除: limactl stop "$vm_name" &>/dev/null |
| 505 | | −删除: limactl delete "$vm_name" --force &>/dev/null |
| 506 | | −删除: _agent_vm_cleanup_state "$vm_name" |
| 347 | +新增: _agent_vm_delete_vm "$vm_name" || return 1 |
| 507 | 348 | fi |
| 508 | 349 | |
| 509 | | −删除: local is_new_vm="" |
| 350 | +新增: local is_new_vm="" apply_resize="" |
| 510 | 351 | local file_mount_entries=() |
| 511 | 352 | local file_mounts_cache="$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}" |
| 512 | 353 | |
| 513 | 354 | if ! _agent_vm_exists "$vm_name"; then |
| 514 | 355 | is_new_vm=1 |
| 515 | 356 | echo "Creating VM '$vm_name'..." |
| 516 | | −删除: limactl clone "$AGENT_VM_TEMPLATE" "$vm_name" --tty=false &>/dev/null |
| 517 | | −删除: # Apply mount and resource settings via edit after clone |
| 518 | | −删除: # Mount and memory/cpus are applied separately from disk, because |
| 519 | | −删除: # Lima rejects the entire edit if disk shrinking is attempted. |
| 520 | | −删除: local mounts_json |
| 521 | | −删除: mounts_json=$(_agent_vm_build_mounts_json "$vm_name" "$host_dir") |
| 522 | | −删除: local edit_args=() |
| 523 | | −删除: edit_args+=(--set ".mounts = ${mounts_json}") |
| 524 | | −删除: [[ -n "$memory" ]] && edit_args+=(--memory "$memory") |
| 525 | | −删除: [[ -n "$cpus" ]] && edit_args+=(--cpus "$cpus") |
| 526 | | −删除: (cd /tmp && limactl edit "$vm_name" ${edit_args[@]+"${edit_args[@]}"}) &>/dev/null |
| 527 | | −删除: if [[ -n "$disk" ]]; then |
| 528 | | −删除: if ! (cd /tmp && limactl edit "$vm_name" --disk "$disk") &>/dev/null; then |
| 529 | | −删除: echo "Warning: Cannot set disk to ${disk} GiB (shrinking is not supported). Re-run 'agent-vm setup --disk ${disk}' for a smaller base." >&2 |
| 530 | | −删除: fi |
| 357 | +新增: local create_out |
| 358 | +新增: if ! create_out=$(limactl clone "$AGENT_VM_TEMPLATE" "$vm_name" --tty=false 2>&1); then |
| 359 | +新增: echo "Error: could not clone the base template into '$vm_name':" >&2 |
| 360 | +新增: echo "$create_out" >&2 |
| 361 | +新增: return 1 |
| 531 | 362 | fi |
| 532 | | −删除: _agent_vm_print_resources "$vm_name" |
| 533 | | −删除: # Record which base version this VM was cloned from |
| 363 | +新增: # Which agent-vm built its base: none recorded for a base of 0.1.0, which |
| 364 | +新增: # has no sshfs for the protected shares. Then migrated before the edit, |
| 365 | +新增: # on the bare clone (see _agent_vm_migrate_0_1). |
| 366 | +新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-built-by-${vm_name}" "$AGENT_VM_STATE_DIR/.agent-vm-sshfs-${vm_name}" |
| 367 | +新增: if [[ -f "$AGENT_VM_STATE_DIR/.agent-vm-base-built-by" ]]; then |
| 368 | +新增: cp "$AGENT_VM_STATE_DIR/.agent-vm-base-built-by" "$AGENT_VM_STATE_DIR/.agent-vm-built-by-${vm_name}" |
| 369 | +新增: fi |
| 370 | +新增: if [[ -n "$protect_git" ]] && ! _agent_vm_migrate_0_1_if_needed "$vm_name"; then |
| 371 | +新增: _agent_vm_delete_vm "$vm_name" |
| 372 | +新增: return 1 |
| 373 | +新增: fi |
| 374 | +新增: # Configured before its first start. A clone that could not be holds |
| 375 | +新增: # nothing yet: it goes, and the next run starts over (#21). |
| 376 | +新增: if ! _agent_vm_apply_shares "$vm_name" "$host_dir" "$want_writable" \ |
| 377 | +新增: || ! _agent_vm_apply_resources "$vm_name"; then |
| 378 | +新增: _agent_vm_delete_vm "$vm_name" |
| 379 | +新增: return 1 |
| 380 | +新增: fi |
| 381 | +新增: printf '%s' "$hooks_notes" |
| 382 | +新增: # Which base it was cloned from, for the stale warning below. |
| 534 | 383 | local base_ver="$AGENT_VM_STATE_DIR/.agent-vm-base-version" |
| 535 | 384 | if [[ -f "$base_ver" ]]; then |
| 536 | 385 | cp "$base_ver" "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 537 | 386 | fi |
| 538 | | −删除: elif [[ -n "$disk" || -n "$memory" || -n "$cpus" ]] \ |
| 539 | | −删除: && _agent_vm_resources_differ "$vm_name" "$cpus" "$memory" "$disk"; then |
| 540 | | −删除: # Resize the existing VM, but only when the request actually differs from |
| 541 | | −删除: # what the VM already has. Prompting on the mere *presence* of a resource |
| 542 | | −删除: # flag means every caller that passes its defaults on each invocation gets |
| 543 | | −删除: # "Stop the VM and apply changes?" forever, for a no-op. |
| 387 | +新增: elif { [[ -n "$disk" || -n "$memory" || -n "$cpus" ]] \ |
| 388 | +新增: && _agent_vm_resources_differ "$vm_name" "$cpus" "$memory" "$disk"; } \ |
| 389 | +新增: || _agent_vm_ssh_port_differs "$vm_name" "$ssh_port"; then |
| 390 | +新增: # Only when the request differs from what the VM has: a caller passing |
| 391 | +新增: # its defaults every time would otherwise be asked every time. Declined, |
| 392 | +新增: # the VM keeps its settings and everything below still applies. |
| 393 | +新增: apply_resize=1 |
| 544 | 394 | if _agent_vm_running "$vm_name"; then |
| 545 | | −删除: echo "VM '$vm_name' is currently running. It must be stopped to apply new resource settings." |
| 546 | | −删除: printf "Stop the VM and apply changes? [y/N] " >&2 |
| 547 | | −删除: local reply="" |
| 548 | | −删除: IFS= read -r reply </dev/tty 2>/dev/null || reply="" |
| 549 | | −删除: if [[ ! "$reply" =~ ^[Yy]$ ]]; then |
| 550 | | −删除: echo "Aborted. Starting with current settings." |
| 551 | | −删除: return 0 |
| 552 | | −删除: fi |
| 553 | | −删除: echo "Stopping VM..." |
| 554 | | −删除: limactl stop "$vm_name" &>/dev/null |
| 555 | | −删除: fi |
| 556 | | −删除: echo "Updating VM resources..." |
| 557 | | −删除: # Don't touch .mounts here — those are baked in at creation (including any |
| 558 | | −删除: # entries from ~/.agent-vm/volumes). Re-setting them would clobber extras. |
| 559 | | −删除: local edit_args=() |
| 560 | | −删除: [[ -n "$memory" ]] && edit_args+=(--memory "$memory") |
| 561 | | −删除: [[ -n "$cpus" ]] && edit_args+=(--cpus "$cpus") |
| 562 | | −删除: # Only call limactl when there is something to set: `limactl edit <vm>` with |
| 563 | | −删除: # no flags drops into $EDITOR, which would hang a non-interactive caller |
| 564 | | −删除: # that passed --disk on its own. |
| 565 | | −删除: if [[ ${#edit_args[@]} -gt 0 ]]; then |
| 566 | | −删除: local edit_output |
| 567 | | −删除: if ! edit_output=$(cd /tmp && limactl edit "$vm_name" "${edit_args[@]}" 2>&1); then |
| 568 | | −删除: echo "Error: Failed to update VM resources:" >&2 |
| 569 | | −删除: echo "$edit_output" >&2 |
| 570 | | −删除: return 1 |
| 571 | | −删除: fi |
| 572 | | −删除: fi |
| 573 | | −删除: if [[ -n "$disk" ]]; then |
| 574 | | −删除: if ! edit_output=$(cd /tmp && limactl edit "$vm_name" --disk "$disk" 2>&1); then |
| 575 | | −删除: echo "Warning: Cannot set disk to ${disk} GiB (shrinking is not supported). Re-run 'agent-vm setup --disk ${disk}' for a smaller base." >&2 |
| 395 | +新增: echo "VM '$vm_name' is currently running. It must be stopped to apply new settings." |
| 396 | +新增: if _agent_vm_can_ask && [[ "$(_agent_vm_ask_yn "Stop the VM and apply changes?" N)" == "1" ]]; then |
| 397 | +新增: echo "Stopping VM..." |
| 398 | +新增: _agent_vm_stop_to_change "$vm_name" "$host_dir" || return 1 |
| 399 | +新增: else |
| 400 | +新增: echo "Not applied: the VM keeps its current settings." |
| 401 | +新增: apply_resize="" |
| 576 | 402 | fi |
| 577 | 403 | fi |
| 578 | | −删除: _agent_vm_print_resources "$vm_name" |
| 404 | +新增: fi |
| 405 | +新增: if [[ -n "$apply_resize" ]]; then |
| 406 | +新增: echo "Updating VM settings..." |
| 407 | +新增: _agent_vm_apply_resources "$vm_name" || return 1 |
| 579 | 408 | fi |
| 580 | 409 | |
| 581 | | −删除: # Warn if this VM was cloned from an older base. `agent-vm info` exposes the |
| 582 | | −删除: # same verdict as vm_stale= so integrators can ask before starting instead of |
| 583 | | −删除: # reading this warning after the fact. |
| 410 | +新增: # The same verdict as `info`'s vm_stale=. |
| 584 | 411 | if [[ "$(_agent_vm_stale_state "$vm_name")" == "1" ]]; then |
| 585 | 412 | echo "Warning: Base VM has been updated since this VM was cloned. Use --reset to re-clone from the new base." >&2 |
| 586 | 413 | fi |
| 587 | 414 | |
| 588 | | −删除: if ! _agent_vm_running "$vm_name"; then |
| 415 | +新增: # Whether another session may be using it: read before the start below. |
| 416 | +新增: local was_running="" |
| 417 | +新增: _agent_vm_running "$vm_name" && was_running=1 |
| 418 | +新增: |
| 419 | +新增: # Shares recorded otherwise than asked: the .git protection (git_stale), its |
| 420 | +新增: # names (names_stale), the mode (mode_stale). A stopped VM is changed before |
| 421 | +新增: # it boots, so nothing starting with it gets a writable window; a running |
| 422 | +新增: # one keeps its shares until stopped, or is offered a restart. |
| 423 | +新增: local was_protected="" git_stale="" names_stale="" mode_stale="" |
| 424 | +新增: _agent_vm_mounts_protect_git "$vm_name" && was_protected=1 |
| 425 | +新增: [[ "$was_protected" != "$protect_git" ]] && git_stale=1 |
| 426 | +新增: if [[ -n "$protect_git" && -n "$was_protected" ]] \ |
| 427 | +新增: && ! _agent_vm_mounts_have_readonly_names "$vm_name" "$ro_names"; then |
| 428 | +新增: names_stale=1 |
| 429 | +新增: fi |
| 430 | +新增: if [[ -n "$scratch" ]]; then |
| 431 | +新增: # Shares nothing, as made: nothing to bring in line. |
| 432 | +新增: git_stale="" |
| 433 | +新增: elif [[ "$want_writable" == "false" ]]; then |
| 434 | +新增: _agent_vm_mounts_all_readonly "$vm_name" || mode_stale=1 |
| 435 | +新增: elif _agent_vm_mounts_all_readonly "$vm_name"; then |
| 436 | +新增: mode_stale=1 |
| 437 | +新增: fi |
| 438 | +新增: # A running VM lacking a protection it should have: restarting it cuts its |
| 439 | +新增: # other sessions, so it is asked; declined, it is a risk to accept. Not |
| 440 | +新增: # under --readonly, which restarts it below anyway. |
| 441 | +新增: if [[ -z "$rdonly" && -n "$was_running" && -n "$protect_git" && ( -n "$git_stale" || -n "$names_stale" ) ]]; then |
| 442 | +新增: if [[ -n "$git_stale" ]]; then |
| 443 | +新增: echo "Warning: VM '$vm_name' is running with .git writable, so the agent can still write .git." >&2 |
| 444 | +新增: else |
| 445 | +新增: echo "Warning: VM '$vm_name' runs with an older list of read-only names (it needs $(_agent_vm_names_text "$ro_names")), so the agent can still write the new ones." >&2 |
| 446 | +新增: fi |
| 447 | +新增: if ! _agent_vm_prompts_disabled_by >/dev/null && _agent_vm_can_ask \ |
| 448 | +新增: && [[ "$(_agent_vm_ask_yn "Restart it now to apply them? Sessions using it are cut." Y)" == "1" ]]; then |
| 449 | +新增: _agent_vm_stop_to_change "$vm_name" "$host_dir" || return 1 |
| 450 | +新增: was_running="" |
| 451 | +新增: elif ! _agent_vm_confirm_unsafe; then |
| 452 | +新增: echo "Aborted. 'agent-vm stop', then run again." >&2 |
| 453 | +新增: return 1 |
| 454 | +新增: fi |
| 455 | +新增: fi |
| 456 | +新增: if [[ -n "$git_stale" && -n "$was_running" ]]; then |
| 457 | +新增: if [[ -n "$protect_git" ]]; then |
| 458 | +新增: : |
| 459 | +新增: elif _agent_vm_writable_git_optout; then |
| 460 | +新增: echo "Note: VM '$vm_name' keeps .git read-only until it stops." >&2 |
| 461 | +新增: else |
| 462 | +新增: echo "Warning: this Lima cannot keep .git read-only; VM '$vm_name' keeps its current shares until it stops." >&2 |
| 463 | +新增: fi |
| 464 | +新增: elif [[ -n "$names_stale" && -n "$was_running" ]]; then |
| 465 | +新增: : |
| 466 | +新增: elif [[ -z "$was_running" && ( -n "$git_stale" || -n "$names_stale" || -n "$mode_stale" ) ]]; then |
| 467 | +新增: if [[ -n "$git_stale" ]]; then |
| 468 | +新增: if [[ -n "$protect_git" ]]; then |
| 469 | +新增: echo "Making every .git read-only for VM '$vm_name'..." |
| 470 | +新增: elif _agent_vm_writable_git_optout; then |
| 471 | +新增: echo "Making .git writable for VM '$vm_name' ($(_agent_vm_writable_git_why))..." >&2 |
| 472 | +新增: else |
| 473 | +新增: echo "Warning: this Lima cannot keep .git read-only; VM '$vm_name' goes back to Lima's default mount type." >&2 |
| 474 | +新增: fi |
| 475 | +新增: elif [[ -n "$names_stale" ]]; then |
| 476 | +新增: echo "Making $(_agent_vm_names_text "$ro_names") read-only for VM '$vm_name'..." |
| 477 | +新增: fi |
| 478 | +新增: [[ -n "$git_stale" || -n "$names_stale" ]] && printf '%s' "$hooks_notes" |
| 479 | +新增: if [[ -n "$protect_git" ]]; then |
| 480 | +新增: _agent_vm_migrate_0_1_if_needed "$vm_name" || return 1 |
| 481 | +新增: fi |
| 482 | +新增: if [[ -n "$mode_stale" && "$want_writable" == "false" ]]; then |
| 483 | +新增: echo "Making every share of VM '$vm_name' read-only..." |
| 484 | +新增: elif [[ -n "$mode_stale" ]]; then |
| 485 | +新增: echo "VM '$vm_name' was left read-only by --readonly; making it writable again..." |
| 486 | +新增: fi |
| 487 | +新增: _agent_vm_apply_shares "$vm_name" "$host_dir" "$want_writable" || return 1 |
| 488 | +新增: fi |
| 489 | +新增: |
| 490 | +新增: if [[ -z "$was_running" ]]; then |
| 589 | 491 | echo "Starting VM '$vm_name'..." |
| 590 | 492 | local start_log |
| 591 | 493 | if ! start_log=$(limactl start "$vm_name" 2>&1); then |
| 494 | +新增: local ha_log |
| 495 | +新增: ha_log="$(_agent_vm_lima_home)/$vm_name/ha.stderr.log" |
| 592 | 496 | echo "Error: Failed to start VM '$vm_name'." >&2 |
| 593 | 497 | echo "--- limactl start output ---" >&2 |
| 594 | 498 | echo "$start_log" >&2 |
| 595 | | −删除: echo "Full log: ~/.lima/$vm_name/ha.stderr.log" >&2 |
| 499 | +新增: echo "Full log: $ha_log" >&2 |
| 500 | +新增: _agent_vm_windows_start_hint "$ha_log" |
| 501 | +新增: return 1 |
| 502 | +新增: fi |
| 503 | +新增: fi |
| 504 | +新增: |
| 505 | +新增: # Bring the shares of the running VM in line with the mode asked: a write |
| 506 | +新增: # probe on the project (see _agent_vm_push_env_and_probe, which pushes the |
| 507 | +新增: # env too), and the record for the other shares, a missing one counting as |
| 508 | +新增: # writable. A mismatch is a stop, edit and start: a broken mount repaired, |
| 509 | +新增: # --readonly applied, or a --readonly VM made writable again. |
| 510 | +新增: # --scratch takes nothing of the project but ~/.agent-vm/env, and works at |
| 511 | +新增: # the project's path on the VM's own disk. |
| 512 | +新增: local is_writable="false" probe_out guest_env="" guest_runtime="" project_env="" project_runtime="" env_payload |
| 513 | +新增: if [[ -n "$scratch" ]]; then |
| 514 | +新增: if ! limactl shell "$vm_name" sh -c 'sudo install -d -o "$(id -u)" -g "$(id -g)" "$1"' sh "$host_dir" </dev/null >/dev/null 2>&1; then |
| 515 | +新增: echo "Error: could not make $host_dir in VM '$vm_name'." >&2 |
| 596 | 516 | return 1 |
| 597 | 517 | fi |
| 518 | +新增: env_payload="$( [ ! -f "$AGENT_VM_STATE_DIR/env" ] || _agent_vm_strip_cr < "$AGENT_VM_STATE_DIR/env" )" |
| 519 | +新增: else |
| 520 | +新增: project_env="$(_agent_vm_project_env_file "$host_dir")" |
| 521 | +新增: project_runtime="$(_agent_vm_project_runtime_path "$host_dir")" |
| 522 | +新增: # By their path in the project, the one the VM has. |
| 523 | +新增: local in_rel |
| 524 | +新增: in_rel="$(_agent_vm_project_rel "$host_dir" "$project_env")" && guest_env="$host_dir/$in_rel" |
| 525 | +新增: in_rel="$(_agent_vm_project_rel "$host_dir" "$project_runtime")" && guest_runtime="$host_dir/$in_rel" |
| 526 | +新增: env_payload="$(_agent_vm_env_payload "$host_dir")" |
| 598 | 527 | fi |
| 528 | +新增: _agent_vm_push_and_probe |
| 529 | +新增: # A scratch VM has no share to repair: the repair below would mount the |
| 530 | +新增: # project into it. |
| 531 | +新增: if [[ -n "$scratch" && "$is_writable" != "true" ]]; then |
| 532 | +新增: echo "Error: $host_dir is not writable in VM '$vm_name'." >&2 |
| 533 | +新增: return 1 |
| 534 | +新增: fi |
| 535 | +新增: local needs_remount="" |
| 536 | +新增: [[ "$is_writable" != "$want_writable" ]] && needs_remount=1 |
| 537 | +新增: if [[ "$want_writable" == "false" ]] && ! _agent_vm_mounts_all_readonly "$vm_name"; then |
| 538 | +新增: needs_remount=1 |
| 539 | +新增: fi |
| 540 | +新增: |
| 541 | +新增: if [[ -n "$needs_remount" ]]; then |
| 542 | +新增: # Stopping kills whatever else is using this VM, so ask first rather than |
| 543 | +新增: # yanking a running session out from under another terminal, in both |
| 544 | +新增: # directions. The repair direction does not ask: a broken mount is |
| 545 | +新增: # already unusable. |
| 546 | +新增: if [[ "$want_writable" == "true" && -n "$was_running" ]] && _agent_vm_mounts_all_readonly "$vm_name"; then |
| 547 | +新增: echo "VM '$vm_name' runs read-only (--readonly). It must be restarted to make its shares writable." |
| 548 | +新增: if ! _agent_vm_can_ask || [[ "$(_agent_vm_ask_yn "Stop the VM and make it writable? Sessions using it are cut." N)" != "1" ]]; then |
| 549 | +新增: echo "Error: not restarted. Pass --readonly to use it as it is, or 'agent-vm stop' it first." >&2 |
| 550 | +新增: return 1 |
| 551 | +新增: fi |
| 552 | +新增: elif [[ "$want_writable" == "false" ]] && [[ -n "$was_running" ]]; then |
| 553 | +新增: echo "VM '$vm_name' was already running. It must be restarted to make its shares read-only." |
| 554 | +新增: if ! _agent_vm_can_ask || [[ "$(_agent_vm_ask_yn "Stop the VM and apply --readonly?" N)" != "1" ]]; then |
| 555 | +新增: # Not "continue with current settings" like the resize path does: |
| 556 | +新增: # carrying on writable after --readonly was asked for is the one |
| 557 | +新增: # outcome that must not be silent. |
| 558 | +新增: echo "Error: --readonly was requested but not applied. Aborting." >&2 |
| 559 | +新增: return 1 |
| 560 | +新增: fi |
| 561 | +新增: elif [[ "$want_writable" == "true" ]]; then |
| 562 | +新增: echo "Project mount is not writable; repairing..." >&2 |
| 563 | +新增: fi |
| 599 | 564 | |
| 600 | | −删除: # Sanity-check that the project directory is actually shared and writable. |
| 601 | | −删除: # A silently-failed or stale mount leaves a bare, root-owned mountpoint; |
| 602 | | −删除: # without this check every later write (runtime scripts, the agent itself) |
| 603 | | −删除: # fails with a confusing cascade of "Permission denied" instead of one clear |
| 604 | | −删除: # error. Runs before the optional --readonly remount below, so it reflects |
| 605 | | −删除: # the mount itself, not the requested read-only restriction. |
| 606 | | −删除: # |
| 607 | | −删除: # If the mount is broken, try to self-heal once: re-apply the mount config |
| 608 | | −删除: # (Lima only allows editing a stopped VM) and restart. This repairs a stale |
| 609 | | −删除: # or silently-missing mount without forcing a full --reset, and adds no |
| 610 | | −删除: # overhead on the common path where the mount is already healthy. |
| 611 | | −删除: if ! limactl shell "$vm_name" test -w "$host_dir" &>/dev/null; then |
| 612 | | −删除: echo "Project mount is not writable; repairing..." >&2 |
| 613 | | −删除: limactl stop "$vm_name" &>/dev/null |
| 614 | | −删除: # Rebuild the full mounts JSON so any ~/.agent-vm/volumes entries are |
| 615 | | −删除: # preserved across the repair (a plain project-dir-only set would silently |
| 616 | | −删除: # drop them). |
| 617 | | −删除: local repair_mounts_json |
| 618 | | −删除: repair_mounts_json=$(_agent_vm_build_mounts_json "$vm_name" "$host_dir") |
| 619 | | −删除: (cd /tmp && limactl edit "$vm_name" \ |
| 620 | | −删除: --set ".mounts = ${repair_mounts_json}") &>/dev/null |
| 621 | | −删除: limactl start "$vm_name" &>/dev/null |
| 622 | | −删除: if ! limactl shell "$vm_name" test -w "$host_dir" &>/dev/null; then |
| 623 | | −删除: echo "Error: project directory is still not writable inside the VM:" >&2 |
| 624 | | −删除: echo " $host_dir" >&2 |
| 625 | | −删除: echo "The host mount failed to attach. Try 'agent-vm --reset <command>'" >&2 |
| 626 | | −删除: echo "to re-clone the VM from the base template." >&2 |
| 565 | +新增: # Stopped for sure first: recording read-only shares while it still runs |
| 566 | +新增: # writable would claim what is not true. |
| 567 | +新增: _agent_vm_stop_to_change "$vm_name" "$host_dir" || return 1 |
| 568 | +新增: if [[ -n "$protect_git" ]]; then |
| 569 | +新增: _agent_vm_migrate_0_1_if_needed "$vm_name" || return 1 |
| 570 | +新增: fi |
| 571 | +新增: _agent_vm_apply_shares "$vm_name" "$host_dir" "$want_writable" || return 1 |
| 572 | +新增: # Checked: the probe below cannot tell a dead VM from a read-only one. |
| 573 | +新增: if ! limactl start "$vm_name" &>/dev/null; then |
| 574 | +新增: echo "Error: VM '$vm_name' did not come back up after changing the project mount." >&2 |
| 575 | +新增: return 1 |
| 576 | +新增: fi |
| 577 | +新增: # Again: in the repair case the first probe could not see the project's |
| 578 | +新增: # env file and runtime script. |
| 579 | +新增: _agent_vm_push_and_probe |
| 580 | +新增: if [[ "$is_writable" != "$want_writable" ]]; then |
| 581 | +新增: if [[ "$want_writable" == "true" ]]; then |
| 582 | +新增: echo "Error: project directory is still not writable inside the VM:" >&2 |
| 583 | +新增: echo " $host_dir" >&2 |
| 584 | +新增: echo "The host mount failed to attach. Try 'agent-vm --reset <command>'" >&2 |
| 585 | +新增: echo "to re-clone the VM from the base template." >&2 |
| 586 | +新增: else |
| 587 | +新增: echo "Error: failed to mount the project directory read-only:" >&2 |
| 588 | +新增: echo " $host_dir" >&2 |
| 589 | +新增: fi |
| 627 | 590 | return 1 |
| 628 | 591 | fi |
| 629 | 592 | fi |
| 630 | 593 | |
| 631 | | −删除: # Install the host's terminfo entry inside the VM so non-standard terminals |
| 632 | | −删除: # (xterm-ghostty, xterm-kitty, …) work correctly. Without this, zsh/ZLE |
| 633 | | −删除: # can't decode keys → broken backspace, arrows, etc. Cached per-VM so we only |
| 634 | | −删除: # pay the limactl shell roundtrip when $TERM actually changes. |
| 594 | +新增: # --readonly is only claimed where the host enforces it (see |
| 595 | +新增: # _agent_vm_mount_is_host_enforced); "could not tell" is refused too. |
| 596 | +新增: if [[ -n "$rdonly" ]]; then |
| 597 | +新增: _agent_vm_mount_is_host_enforced "$vm_name" |
| 598 | +新增: case $? in |
| 599 | +新增: 0) echo "Read-only: the project and every other share (enforced on the host)." ;; |
| 600 | +新增: 1) echo "Error: --readonly cannot be enforced with this mount type." >&2 |
| 601 | +新增: echo " Lima only applies it inside the guest for reverse-sshfs (without" >&2 |
| 602 | +新增: echo " readonlyNames) and for virtiofs under QEMU, where root in the VM" >&2 |
| 603 | +新增: echo " can remount it read-write. Use virtiofs on vz, or 9p on QEMU (both" >&2 |
| 604 | +新增: echo " enforce it on the host), in your Lima config, then --reset." >&2 |
| 605 | +新增: return 1 ;; |
| 606 | +新增: *) echo "Error: could not determine the project mount type, so --readonly" >&2 |
| 607 | +新增: echo " cannot be confirmed as enforced outside the VM. Aborting." >&2 |
| 608 | +新增: return 1 ;; |
| 609 | +新增: esac |
| 610 | +新增: fi |
| 611 | +新增: |
| 612 | +新增: # The host's terminfo entry in the VM, for terminals Debian does not know |
| 613 | +新增: # (xterm-ghostty, xterm-kitty): zsh cannot read their keys otherwise. Once |
| 614 | +新增: # per VM and $TERM. |
| 635 | 615 | local term_cache="$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}" |
| 636 | 616 | if [[ -n "${TERM:-}" ]] && [[ "$(cat "$term_cache" 2>/dev/null)" != "$TERM" ]] \ |
| 637 | 617 | && infocmp -x "$TERM" &>/dev/null; then |
| 638 | 618 | if infocmp -x "$TERM" | limactl shell "$vm_name" sudo tic -x - &>/dev/null; then |
| 639 | | −删除: echo "$TERM" > "$term_cache" |
| 619 | +新增: echo "$TERM" >| "$term_cache" |
| 640 | 620 | else |
| 641 | 621 | echo "Warning: failed to install '$TERM' terminfo inside VM." >&2 |
| 642 | 622 | fi |
| 643 | 623 | fi |
| 644 | 624 | |
| 645 | | −删除: # Push ~/.agent-vm/env (a dotenv-style file of tokens / API keys) into the VM |
| 646 | | −删除: # at $HOME/.agent-vm.env on every start, so updates on the host propagate |
| 647 | | −删除: # without --reset. The base VM's ~/.zshenv auto-sources it via `set -a`, so |
| 648 | | −删除: # the contents stay a plain KEY=value file (no `export` needed). `umask 077` |
| 649 | | −删除: # creates the file mode-600 since it usually holds secrets. |
| 650 | | −删除: if [ -f "$AGENT_VM_STATE_DIR/env" ]; then |
| 651 | | −删除: if ! limactl shell "$vm_name" sh -c 'umask 077 && rm -f "$HOME/.agent-vm.env" && cat > "$HOME/.agent-vm.env"' \ |
| 652 | | −删除: < "$AGENT_VM_STATE_DIR/env" 2>/dev/null; then |
| 653 | | −删除: echo "Warning: failed to push ~/.agent-vm/env into VM '$vm_name'." >&2 |
| 654 | | −删除: fi |
| 655 | | −删除: fi |
| 656 | | −删除: |
| 657 | 625 | # Run per-user runtime script if it exists |
| 658 | 626 | if [ -f "$AGENT_VM_STATE_DIR/runtime.sh" ]; then |
| 659 | 627 | echo "Running user runtime setup..." |
| 660 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" zsh -l < "$AGENT_VM_STATE_DIR/runtime.sh" |
| 628 | +新增: _agent_vm_run_runtime "$vm_name" "$host_dir" "$AGENT_VM_STATE_DIR/runtime.sh" |
| 661 | 629 | fi |
| 662 | 630 | |
| 663 | | −删除: # Run project-specific runtime script if it exists |
| 664 | | −删除: if [ -f "${host_dir}/.agent-vm.runtime.sh" ]; then |
| 665 | | −删除: echo "Running project runtime setup..." |
| 666 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" zsh -l < "${host_dir}/.agent-vm.runtime.sh" |
| 667 | | −删除: fi |
| 668 | | −删除: |
| 669 | | −删除: # Apply per-session restrictions |
| 670 | | −删除: if [[ -n "$offline" ]]; then |
| 671 | | −删除: echo "Enabling offline mode..." |
| 672 | | −删除: limactl shell "$vm_name" sudo iptables -F OUTPUT 2>/dev/null |
| 673 | | −删除: limactl shell "$vm_name" sudo iptables -A OUTPUT -o lo -j ACCEPT |
| 674 | | −删除: limactl shell "$vm_name" sudo iptables -A OUTPUT -d 10.0.0.0/8 -j ACCEPT |
| 675 | | −删除: limactl shell "$vm_name" sudo iptables -A OUTPUT -d 172.16.0.0/12 -j ACCEPT |
| 676 | | −删除: limactl shell "$vm_name" sudo iptables -A OUTPUT -d 192.168.0.0/16 -j ACCEPT |
| 677 | | −删除: limactl shell "$vm_name" sudo iptables -P OUTPUT DROP |
| 678 | | −删除: fi |
| 679 | | −删除: |
| 680 | | −删除: if [[ -n "$rdonly" ]]; then |
| 681 | | −删除: # `mount -o remount,ro` on the host share fails silently when Lima uses |
| 682 | | −删除: # a FUSE-backed mount (sshfs/virtiofs in some configurations) — FUSE |
| 683 | | −删除: # rejects the `remount` option and the FS stays writable. Self-bind the |
| 684 | | −删除: # directory first so we have a plain Linux bind-mount layer to remount, |
| 685 | | −删除: # which works regardless of what's underneath. |
| 686 | | −删除: echo "Mounting project directory as read-only..." |
| 687 | | −删除: if ! limactl shell "$vm_name" sudo mount --bind "$host_dir" "$host_dir" \ |
| 688 | | −删除: || ! limactl shell "$vm_name" sudo mount -o remount,ro,bind "$host_dir"; then |
| 689 | | −删除: echo "Error: Failed to mount project directory as read-only." >&2 |
| 690 | | −删除: return 1 |
| 691 | | −删除: fi |
| 692 | | −删除: fi |
| 693 | | −删除: |
| 694 | | −删除: if [[ -n "$git_ro" ]] && [[ -d "$host_dir/.git" ]]; then |
| 695 | | −删除: echo "Mounting .git directory as read-only..." |
| 696 | | −删除: if ! limactl shell "$vm_name" sudo mount --bind "$host_dir/.git" "$host_dir/.git" \ |
| 697 | | −删除: || ! limactl shell "$vm_name" sudo mount -o remount,ro,bind "$host_dir/.git"; then |
| 698 | | −删除: echo "Error: Failed to mount .git directory as read-only." >&2 |
| 699 | | −删除: return 1 |
| 631 | +新增: # Run project-specific runtime script if it exists: by its path in the VM |
| 632 | +新增: # when it is in the project (the probe above said whether it is there), |
| 633 | +新增: # piped from the host when AGENT_VM_PROJECT_RUNTIME puts it elsewhere. |
| 634 | +新增: if [[ -n "$guest_runtime" ]]; then |
| 635 | +新增: if [[ "$probe_out" == *runtime-found* ]]; then |
| 636 | +新增: echo "Running project runtime setup..." |
| 637 | +新增: _agent_vm_run_project_runtime "$vm_name" "$host_dir" "$guest_runtime" |
| 700 | 638 | fi |
| 639 | +新增: elif [ -f "$project_runtime" ]; then |
| 640 | +新增: echo "Running project runtime setup..." |
| 641 | +新增: _agent_vm_run_runtime "$vm_name" "$host_dir" "$project_runtime" |
| 701 | 642 | fi |
| 702 | 643 | |
| 703 | | −删除: # Load file mount entries from the cache. _agent_vm_build_mounts_json writes |
| 704 | | −删除: # them there (from its own local scope) for both new and existing VMs, so we |
| 705 | | −删除: # always read them back here to drive the inside-VM bind mounts below. |
| 644 | +新增: # Single files of ~/.agent-vm/volumes (see _agent_vm_build_mounts_json, which |
| 645 | +新增: # lists them in the cache): an existing VM's staged copy refreshed, so an |
| 646 | +新增: # edit saved by rename on the host shows, then each bound at its place in |
| 647 | +新增: # the VM, in one round trip. The bind does not survive a restart, and is |
| 648 | +新增: # skipped when there. |
| 706 | 649 | if [[ -f "$file_mounts_cache" ]]; then |
| 707 | | −删除: local entry |
| 708 | | −删除: while IFS= read -r entry; do |
| 709 | | −删除: [[ -n "$entry" ]] && file_mount_entries+=("$entry") |
| 710 | | −删除: done < "$file_mounts_cache" |
| 711 | | −删除: |
| 712 | | −删除: # For existing VMs, refresh host-side hardlinks so atomic-rename edits on the |
| 713 | | −删除: # host propagate after a VM restart (ln/cp against the cached staging path). |
| 714 | | −删除: # New VMs just staged fresh copies in _agent_vm_build_mounts_json, so there |
| 715 | | −删除: # is nothing to refresh. |
| 716 | | −删除: if [[ -z "$is_new_vm" ]] && [[ ${#file_mount_entries[@]} -gt 0 ]]; then |
| 717 | | −删除: local host_src host_staging _bind_src _bind_dst |
| 718 | | −删除: for entry in "${file_mount_entries[@]}"; do |
| 719 | | −删除: IFS='|' read -r host_src host_staging _bind_src _bind_dst <<< "$entry" |
| 720 | | −删除: [[ -z "$host_staging" ]] && continue |
| 721 | | −删除: if [[ ! -e "$host_src" ]]; then |
| 722 | | −删除: echo "Warning: Mount source '${host_src}' no longer exists; VM will see the last-staged copy." >&2 |
| 723 | | −删除: continue |
| 724 | | −删除: fi |
| 650 | +新增: local host_src host_staging bind_src bind_dst |
| 651 | +新增: while IFS='|' read -r host_src host_staging bind_src bind_dst; do |
| 652 | +新增: [[ -n "$host_staging" ]] || continue |
| 653 | +新增: if [[ -n "$is_new_vm" ]]; then |
| 654 | +新增: : |
| 655 | +新增: elif [[ ! -e "$host_src" ]]; then |
| 656 | +新增: echo "Warning: Mount source '${host_src}' no longer exists; VM will see the last-staged copy." >&2 |
| 657 | +新增: else |
| 725 | 658 | _agent_vm_stage_file "$host_src" "$host_staging" \ |
| 726 | 659 | || echo "Warning: Failed to refresh staged '${host_src}'; VM may see stale content." >&2 |
| 727 | | −删除: done |
| 728 | | −删除: fi |
| 729 | | −删除: fi |
| 730 | | −删除: |
| 731 | | −删除: # Apply inside-VM bind mounts so each staged file appears at its final path. |
| 732 | | −删除: # Lima re-mounts staging dirs on each start, but the bind onto the final dest |
| 733 | | −删除: # is ephemeral. Batched into one limactl shell call (roundtrips cost ~1-2s) |
| 734 | | −删除: # and made idempotent so re-runs on a running VM are cheap no-ops. |
| 735 | | −删除: if [[ ${#file_mount_entries[@]} -gt 0 ]]; then |
| 736 | | −删除: local file_bind_payload=() |
| 737 | | −删除: local _host_src _host_staging bind_src bind_dst |
| 738 | | −删除: for entry in "${file_mount_entries[@]}"; do |
| 739 | | −删除: IFS='|' read -r _host_src _host_staging bind_src bind_dst <<< "$entry" |
| 740 | | −删除: [[ -n "$bind_src" && -n "$bind_dst" ]] && file_bind_payload+=("${bind_src}|${bind_dst}") |
| 741 | | −删除: done |
| 742 | | −删除: if [[ ${#file_bind_payload[@]} -gt 0 ]]; then |
| 660 | +新增: fi |
| 661 | +新增: [[ -n "$bind_src" && -n "$bind_dst" ]] && file_mount_entries+=("${bind_src}|${bind_dst}") |
| 662 | +新增: done < "$file_mounts_cache" |
| 663 | +新增: if [[ ${#file_mount_entries[@]} -gt 0 ]]; then |
| 743 | 664 | echo "Mounting individual files..." |
| 744 | | −删除: # Paths are passed as positional args (single-quoted script) so entries |
| 745 | | −删除: # containing quotes or metacharacters cannot be interpreted as shell code. |
| 665 | +新增: # The paths as arguments, never in the script. |
| 746 | 666 | limactl shell "$vm_name" sudo bash -c ' |
| 747 | 667 | set -e |
| 748 | 668 | for entry in "$@"; do |
| @@ -754,64 +674,44 @@ _agent_vm_ensure_running() { |
| 754 | 674 | mount -o remount,ro,bind "$bind_dst" |
| 755 | 675 | fi |
| 756 | 676 | done |
| 757 | | −删除: ' -- "${file_bind_payload[@]}" |
| 677 | +新增: ' -- "${file_mount_entries[@]}" |
| 758 | 678 | fi |
| 759 | 679 | fi |
| 760 | 680 | } |
| 761 | 681 | |
| 682 | +新增:# --- agent-vm: the command, dispatching to the rest --------------------------- |
| 683 | +新增: |
| 762 | 684 | agent-vm() { |
| 763 | | −删除: local vm_opts=() |
| 764 | | −删除: # Parse global options before the subcommand. |
| 765 | | −删除: # |
| 766 | | −删除: # Resource values are validated here, as `setup` already does for its own |
| 767 | | −删除: # flags. Without it a typo like `--disk 10G` travels all the way into the |
| 768 | | −删除: # resource comparison and surfaces as a raw bash diagnostic |
| 769 | | −删除: # ("[[: 10G: value too great for base") before anything actionable is said. |
| 685 | +新增: # Options before the command are checked here and handed to it as written: |
| 686 | +新增: # the commands that start a VM read them again, with their own. |
| 687 | +新增: local lead=() vm_opts=() rm="" taken |
| 770 | 688 | while [[ $# -gt 0 ]]; do |
| 771 | | −删除: case "$1" in |
| 772 | | −删除: --disk) |
| 773 | | −删除: _agent_vm_validate_int --disk "$2" || return 1 |
| 774 | | −删除: vm_opts+=(--disk "$2"); shift 2 ;; |
| 775 | | −删除: --disk=*) |
| 776 | | −删除: _agent_vm_validate_int --disk "${1#*=}" || return 1 |
| 777 | | −删除: vm_opts+=(--disk "${1#*=}"); shift ;; |
| 778 | | −删除: --memory|--ram) |
| 779 | | −删除: _agent_vm_validate_int --memory "$2" || return 1 |
| 780 | | −删除: vm_opts+=(--memory "$2"); shift 2 ;; |
| 781 | | −删除: --memory=*|--ram=*) |
| 782 | | −删除: _agent_vm_validate_int --memory "${1#*=}" || return 1 |
| 783 | | −删除: vm_opts+=(--memory "${1#*=}"); shift ;; |
| 784 | | −删除: --cpus) |
| 785 | | −删除: _agent_vm_validate_int --cpus "$2" || return 1 |
| 786 | | −删除: vm_opts+=(--cpus "$2"); shift 2 ;; |
| 787 | | −删除: --cpus=*) |
| 788 | | −删除: _agent_vm_validate_int --cpus "${1#*=}" || return 1 |
| 789 | | −删除: vm_opts+=(--cpus "${1#*=}"); shift ;; |
| 790 | | −删除: --reset) |
| 791 | | −删除: vm_opts+=(--reset); shift ;; |
| 792 | | −删除: --offline) |
| 793 | | −删除: vm_opts+=(--offline); shift ;; |
| 794 | | −删除: --readonly) |
| 795 | | −删除: vm_opts+=(--readonly); shift ;; |
| 796 | | −删除: --git-read-only|--git-ro) |
| 797 | | −删除: vm_opts+=(--git-read-only); shift ;; |
| 798 | | −删除: --rm) |
| 799 | | −删除: vm_opts+=(--rm); shift ;; |
| 800 | | −删除: *) |
| 801 | | −删除: break ;; |
| 802 | | −删除: esac |
| 689 | +新增: _agent_vm_take_opt "$@" || return 1 |
| 690 | +新增: [[ $taken -gt 0 ]] || break |
| 691 | +新增: lead+=("${@:1:$taken}") |
| 692 | +新增: shift "$taken" |
| 803 | 693 | done |
| 804 | 694 | |
| 805 | 695 | local cmd="${1:-help}" |
| 806 | 696 | shift 2>/dev/null || true |
| 807 | 697 | |
| 698 | +新增: # VM options mean nothing to the other commands: `agent-vm --readonly stop` |
| 699 | +新增: # must not read as if something had been made read-only. |
| 700 | +新增: if [[ ${#lead[@]} -gt 0 ]]; then |
| 701 | +新增: case "$cmd" in |
| 702 | +新增: stop|rm|destroy|destroy-all|list|status|name|info|env|project-env|version|--version|-V|doctor|install|uninstall|help|--help|-h) |
| 703 | +新增: echo "Error: ${lead[*]:0:1} is an option for the commands that start a VM (claude, opencode, codex, vibe, pi, shell, run), not for '$cmd'." >&2 |
| 704 | +新增: return 1 ;; |
| 705 | +新增: esac |
| 706 | +新增: fi |
| 707 | +新增: |
| 808 | 708 | # Every command except help/setup needs limactl present. (setup installs it |
| 809 | 709 | # itself; help needs nothing.) Without this, stop/list/status/etc. would fail |
| 810 | 710 | # with confusing empty output instead of a clear, actionable message. |
| 811 | 711 | case "$cmd" in |
| 812 | | −删除: help|--help|-h|setup|version|--version|-V|name|info|env) ;; |
| 712 | +新增: help|--help|-h|setup|version|--version|-V|name|info|env|project-env|doctor|install|uninstall) ;; |
| 813 | 713 | *) |
| 814 | | −删除: if ! command -v limactl &>/dev/null; then |
| 714 | +新增: if [[ -z "$(_agent_vm_limactl_path)" ]]; then |
| 815 | 715 | echo "Error: limactl (Lima) not found. Run 'agent-vm setup' first, or install" >&2 |
| 816 | 716 | echo "it from https://lima-vm.io/docs/installation/" >&2 |
| 817 | 717 | return 1 |
| @@ -820,25 +720,16 @@ agent-vm() { |
| 820 | 720 | |
| 821 | 721 | case "$cmd" in |
| 822 | 722 | setup) |
| 823 | | −删除: _agent_vm_setup ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 824 | | −删除: ;; |
| 825 | | −删除: claude) |
| 826 | | −删除: _agent_vm_claude ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 827 | | −删除: ;; |
| 828 | | −删除: opencode) |
| 829 | | −删除: _agent_vm_opencode ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 830 | | −删除: ;; |
| 831 | | −删除: codex) |
| 832 | | −删除: _agent_vm_codex ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 723 | +新增: _agent_vm_setup ${lead[@]+"${lead[@]}"} "$@" |
| 833 | 724 | ;; |
| 834 | | −删除: vibe) |
| 835 | | −删除: _agent_vm_vibe ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 725 | +新增: claude|opencode|codex|vibe|pi) |
| 726 | +新增: _agent_vm_agent "$cmd" ${lead[@]+"${lead[@]}"} "$@" |
| 836 | 727 | ;; |
| 837 | 728 | shell|sh) |
| 838 | | −删除: _agent_vm_shell ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 729 | +新增: _agent_vm_shell ${lead[@]+"${lead[@]}"} "$@" |
| 839 | 730 | ;; |
| 840 | 731 | run) |
| 841 | | −删除: _agent_vm_run ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 732 | +新增: _agent_vm_run ${lead[@]+"${lead[@]}"} "$@" |
| 842 | 733 | ;; |
| 843 | 734 | stop) |
| 844 | 735 | _agent_vm_stop "$@" |
| @@ -849,11 +740,8 @@ agent-vm() { |
| 849 | 740 | destroy-all) |
| 850 | 741 | _agent_vm_destroy_all "$@" |
| 851 | 742 | ;; |
| 852 | | −删除: list) |
| 853 | | −删除: _agent_vm_list "$@" |
| 854 | | −删除: ;; |
| 855 | | −删除: status) |
| 856 | | −删除: _agent_vm_status "$@" |
| 743 | +新增: list|status) |
| 744 | +新增: _agent_vm_list |
| 857 | 745 | ;; |
| 858 | 746 | name) |
| 859 | 747 | local name_dir |
| @@ -866,10 +754,37 @@ agent-vm() { |
| 866 | 754 | _agent_vm_info "$info_dir" |
| 867 | 755 | ;; |
| 868 | 756 | env) |
| 869 | | −删除: _agent_vm_env "$@" |
| 757 | +新增: _agent_vm_env env "$AGENT_VM_STATE_DIR/env" "" "$@" |
| 758 | +新增: ;; |
| 759 | +新增: project-env) |
| 760 | +新增: local project_env_file project_top="" project_rel |
| 761 | +新增: project_env_file="$(_agent_vm_project_env_file)" |
| 762 | +新增: if project_rel="$(_agent_vm_project_rel "$(pwd)" "$project_env_file")"; then |
| 763 | +新增: project_top="$(pwd)" |
| 764 | +新增: project_env_file="$project_top/$project_rel" |
| 765 | +新增: fi |
| 766 | +新增: _agent_vm_env project-env "$project_env_file" "$project_top" "$@" || return $? |
| 767 | +新增: # Only after a write, and only if it worked: that is when the file is |
| 768 | +新增: # new to the repository and when the user is looking. |
| 769 | +新增: if [ "${1:-}" = "set" ]; then |
| 770 | +新增: _agent_vm_warn_unignored "$project_env_file" |
| 771 | +新增: fi |
| 870 | 772 | ;; |
| 871 | 773 | version|--version|-V) |
| 872 | | −删除: echo "$AGENT_VM_VERSION" |
| 774 | +新增: _agent_vm_version "$@" |
| 775 | +新增: ;; |
| 776 | +新增: doctor) |
| 777 | +新增: if [[ $# -gt 0 ]]; then |
| 778 | +新增: echo "Usage: agent-vm doctor" >&2 |
| 779 | +新增: return 2 |
| 780 | +新增: fi |
| 781 | +新增: _agent_vm_doctor |
| 782 | +新增: ;; |
| 783 | +新增: install) |
| 784 | +新增: _agent_vm_install "$@" |
| 785 | +新增: ;; |
| 786 | +新增: uninstall) |
| 787 | +新增: _agent_vm_uninstall "$@" |
| 873 | 788 | ;; |
| 874 | 789 | help|--help|-h) |
| 875 | 790 | _agent_vm_help |
| @@ -882,654 +797,7 @@ agent-vm() { |
| 882 | 797 | esac |
| 883 | 798 | } |
| 884 | 799 | |
| 885 | | −删除:# Machine-readable state, one key=value per line. This is the supported way for |
| 886 | | −删除:# another tool to ask what agent-vm knows, instead of reverse-engineering VM |
| 887 | | −删除:# naming, the template name, or the state-dir version markers — all of which |
| 888 | | −删除:# are internal and free to change. |
| 889 | | −删除:# |
| 890 | | −删除:# Keys: version, template, state_dir, dir, vm_name, base_exists, vm_exists, |
| 891 | | −删除:# vm_running, vm_stale. Booleans are 1/0; anything that cannot be determined is |
| 892 | | −删除:# "unknown" rather than a guess. |
| 893 | | −删除:_agent_vm_info() { |
| 894 | | −删除: local dir="${1:-$(pwd)}" |
| 895 | | −删除: local vm_name |
| 896 | | −删除: vm_name="$(_agent_vm_name "$dir")" |
| 897 | | −删除: |
| 898 | | −删除: echo "version=$AGENT_VM_VERSION" |
| 899 | | −删除: echo "template=$AGENT_VM_TEMPLATE" |
| 900 | | −删除: echo "state_dir=$AGENT_VM_STATE_DIR" |
| 901 | | −删除: echo "dir=$dir" |
| 902 | | −删除: echo "vm_name=$vm_name" |
| 903 | | −删除: |
| 904 | | −删除: if ! command -v limactl &>/dev/null; then |
| 905 | | −删除: # Still useful without Lima: the static keys above answer "what would this |
| 906 | | −删除: # VM be called", which is all a caller needs before setup has ever run. |
| 907 | | −删除: echo "base_exists=unknown" |
| 908 | | −删除: echo "vm_exists=unknown" |
| 909 | | −删除: echo "vm_running=unknown" |
| 910 | | −删除: echo "vm_stale=unknown" |
| 911 | | −删除: return 0 |
| 912 | | −删除: fi |
| 913 | | −删除: |
| 914 | | −删除: # Keep the three-way answer from the query helpers: a failed query reports |
| 915 | | −删除: # `unknown`, never a confident 0. A caller told `base_exists=0` when the truth |
| 916 | | −删除: # was "could not ask" would offer to build a base VM that already exists. |
| 917 | | −删除: local base_exists vm_exists vm_running |
| 918 | | −删除: _agent_vm_base_exists; base_exists="$(_agent_vm_tristate $?)" |
| 919 | | −删除: _agent_vm_exists "$vm_name"; vm_exists="$(_agent_vm_tristate $?)" |
| 920 | | −删除: _agent_vm_running "$vm_name"; vm_running="$(_agent_vm_tristate $?)" |
| 921 | | −删除: echo "base_exists=$base_exists" |
| 922 | | −删除: echo "vm_exists=$vm_exists" |
| 923 | | −删除: echo "vm_running=$vm_running" |
| 924 | | −删除: # Staleness compares this VM against the base it was cloned from; with no VM |
| 925 | | −删除: # (or no way to tell) there is nothing to compare. |
| 926 | | −删除: if [[ "$vm_exists" == "1" ]]; then |
| 927 | | −删除: echo "vm_stale=$(_agent_vm_stale_state "$vm_name")" |
| 928 | | −删除: else |
| 929 | | −删除: echo "vm_stale=unknown" |
| 930 | | −删除: fi |
| 931 | | −删除:} |
| 932 | | −删除: |
| 933 | | −删除:# Escape a value for a single-quoted shell literal: ' becomes '"'"'. |
| 934 | | −删除:# |
| 935 | | −删除:# Via sed, NOT `${v//\'/\'\"\'\"\'}`: bash 3.2 — what macOS ships — keeps the |
| 936 | | −删除:# backslashes in the replacement half of that substitution and emits |
| 937 | | −删除:# `O\'"\'"\'Brien`. The resulting line is a syntax error, and a shell sourcing |
| 938 | | −删除:# ~/.agent-vm.env then abandons the WHOLE file, losing every secret in it, not |
| 939 | | −删除:# just the one with the quote. |
| 940 | | −删除:_agent_vm_sq_escape() { |
| 941 | | −删除: printf '%s' "$1" | sed "s/'/'\"'\"'/g" |
| 942 | | −删除:} |
| 943 | | −删除: |
| 944 | | −删除:# Read, write and delete entries in ~/.agent-vm/env — the dotenv file pushed |
| 945 | | −删除:# into every VM on each start and auto-sourced there. |
| 946 | | −删除:# |
| 947 | | −删除:# Exists so integrators don't hand-roll the quoting: the file is *sourced* by a |
| 948 | | −删除:# shell, so one bad escape costs every secret in it (see _agent_vm_sq_escape). |
| 949 | | −删除:# |
| 950 | | −删除:# agent-vm env set KEY VALUE replace or add KEY (value never echoed) |
| 951 | | −删除:# agent-vm env get KEY print KEY's value |
| 952 | | −删除:# agent-vm env has KEY exit 0 if KEY is set, 1 otherwise (no output) |
| 953 | | −删除:# agent-vm env unset KEY remove KEY |
| 954 | | −删除:# agent-vm env list print the key NAMES only, never the values |
| 955 | | −删除:# |
| 956 | | −删除:# Writes are atomic (temp file then mv) and the file is kept mode 600. Lines |
| 957 | | −删除:# this command does not manage are preserved untouched. |
| 958 | | −删除:_agent_vm_env() { |
| 959 | | −删除: local action="${1:-list}" |
| 960 | | −删除: local key="${2:-}" |
| 961 | | −删除: local file="$AGENT_VM_STATE_DIR/env" |
| 962 | | −删除: |
| 963 | | −删除: case "$action" in |
| 964 | | −删除: set|get|has|unset) |
| 965 | | −删除: if [[ -z "$key" ]]; then |
| 966 | | −删除: echo "Error: 'agent-vm env $action' needs a KEY." >&2 |
| 967 | | −删除: return 1 |
| 968 | | −删除: fi |
| 969 | | −删除: # A key must be a shell-assignable name: anything else would produce a |
| 970 | | −删除: # line that breaks the file for every reader. |
| 971 | | −删除: if [[ ! "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then |
| 972 | | −删除: echo "Error: '$key' is not a valid environment variable name." >&2 |
| 973 | | −删除: return 1 |
| 974 | | −删除: fi ;; |
| 975 | | −删除: esac |
| 976 | | −删除: |
| 977 | | −删除: case "$action" in |
| 978 | | −删除: set) |
| 979 | | −删除: if [[ $# -lt 3 ]]; then |
| 980 | | −删除: echo "Error: 'agent-vm env set' needs a VALUE." >&2 |
| 981 | | −删除: return 1 |
| 982 | | −删除: fi |
| 983 | | −删除: local value="$3" tmp line |
| 984 | | −删除: mkdir -p "$AGENT_VM_STATE_DIR" |
| 985 | | −删除: tmp="$(mktemp "${file}.XXXXXX")" |
| 986 | | −删除: chmod 600 "$tmp" |
| 987 | | −删除: if [[ -f "$file" ]]; then |
| 988 | | −删除: while IFS= read -r line || [[ -n "$line" ]]; do |
| 989 | | −删除: case "$line" in |
| 990 | | −删除: "${key}="*) : ;; |
| 991 | | −删除: *) printf '%s\n' "$line" >> "$tmp" ;; |
| 992 | | −删除: esac |
| 993 | | −删除: done < "$file" |
| 994 | | −删除: fi |
| 995 | | −删除: printf "%s='%s'\n" "$key" "$(_agent_vm_sq_escape "$value")" >> "$tmp" |
| 996 | | −删除: # A silently-dropped write here means the caller is told the secret was |
| 997 | | −删除: # stored when it was not — the worst possible failure for this file. |
| 998 | | −删除: if ! mv "$tmp" "$file"; then |
| 999 | | −删除: rm -f "$tmp" |
| 1000 | | −删除: echo "Error: could not write $file" >&2 |
| 1001 | | −删除: return 1 |
| 1002 | | −删除: fi |
| 1003 | | −删除: chmod 600 "$file" |
| 1004 | | −删除: ;; |
| 1005 | | −删除: unset) |
| 1006 | | −删除: [[ -f "$file" ]] || return 0 |
| 1007 | | −删除: local tmp line |
| 1008 | | −删除: tmp="$(mktemp "${file}.XXXXXX")" |
| 1009 | | −删除: chmod 600 "$tmp" |
| 1010 | | −删除: while IFS= read -r line || [[ -n "$line" ]]; do |
| 1011 | | −删除: case "$line" in |
| 1012 | | −删除: "${key}="*) : ;; |
| 1013 | | −删除: *) printf '%s\n' "$line" >> "$tmp" ;; |
| 1014 | | −删除: esac |
| 1015 | | −删除: done < "$file" |
| 1016 | | −删除: if ! mv "$tmp" "$file"; then |
| 1017 | | −删除: rm -f "$tmp" |
| 1018 | | −删除: echo "Error: could not write $file" >&2 |
| 1019 | | −删除: return 1 |
| 1020 | | −删除: fi |
| 1021 | | −删除: chmod 600 "$file" |
| 1022 | | −删除: ;; |
| 1023 | | −删除: get|has) |
| 1024 | | −删除: [[ -f "$file" ]] || return 1 |
| 1025 | | −删除: # Source in a subshell rather than parse: the file is shell, so this is |
| 1026 | | −删除: # the only reading that agrees with how the VM will interpret it. |
| 1027 | | −删除: # |
| 1028 | | −删除: # `unset` first: the subshell inherits this shell's environment, so |
| 1029 | | −删除: # without it a variable that merely happens to be exported here would be |
| 1030 | | −删除: # reported as stored in the file. An integrator asking "is GH_TOKEN |
| 1031 | | −删除: # saved?" would get a false yes — and the caller of agent-vm is very |
| 1032 | | −删除: # often a VM that already has these very variables in its environment. |
| 1033 | | −删除: # |
| 1034 | | −删除: # Presence travels as the exit status and the value as stdout, tested with |
| 1035 | | −删除: # ${key+x}. Comparing the value against a sentinel string instead would |
| 1036 | | −删除: # report a variable whose stored value happens to equal that sentinel as |
| 1037 | | −删除: # absent. |
| 1038 | | −删除: local value |
| 1039 | | −删除: if ! value="$(unset "$key" |
| 1040 | | −删除: set -a; . "$file" >/dev/null 2>&1; set +a |
| 1041 | | −删除: eval "[ \"\${${key}+x}\" = x ] || exit 1 |
| 1042 | | −删除: printf '%s' \"\${${key}}\"")"; then |
| 1043 | | −删除: return 1 |
| 1044 | | −删除: fi |
| 1045 | | −删除: [[ "$action" == "has" ]] && return 0 |
| 1046 | | −删除: printf '%s\n' "$value" |
| 1047 | | −删除: ;; |
| 1048 | | −删除: list) |
| 1049 | | −删除: [[ -f "$file" ]] || return 0 |
| 1050 | | −删除: # Names only — never values, so this stays safe to paste into an issue. |
| 1051 | | −删除: sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "$file" |
| 1052 | | −删除: ;; |
| 1053 | | −删除: *) |
| 1054 | | −删除: echo "Usage: agent-vm env {set KEY VALUE|get KEY|has KEY|unset KEY|list}" >&2 |
| 1055 | | −删除: return 1 ;; |
| 1056 | | −删除: esac |
| 1057 | | −删除:} |
| 1058 | | −删除: |
| 1059 | | −删除:_agent_vm_help() { |
| 1060 | | −删除: cat << 'EOF' |
| 1061 | | −删除:Usage: agent-vm [options] <command> [args] |
| 1062 | | −删除: |
| 1063 | | −删除:Commands: |
| 1064 | | −删除: setup Create the base VM template (run once) |
| 1065 | | −删除: claude [args] Run Claude Code in the VM for the current directory |
| 1066 | | −删除: opencode [args] Run OpenCode in the VM for the current directory |
| 1067 | | −删除: codex [args] Run Codex CLI in the VM for the current directory |
| 1068 | | −删除: vibe [args] Run Mistral Vibe in the VM for the current directory |
| 1069 | | −删除: shell, sh Open a shell in the VM. Add -c "..." to run a one-shot |
| 1070 | | −删除: command via login zsh and exit. |
| 1071 | | −删除: run <cmd> [args] Run a command in the VM (no shell — for pipes/redirects |
| 1072 | | −删除: use 'shell -c "..."' instead; pass --tty for TUIs like |
| 1073 | | −删除: opencode, vibe, htop, etc.) |
| 1074 | | −删除: stop Stop the VM for the current directory |
| 1075 | | −删除: rm Stop and delete the VM for the current directory |
| 1076 | | −删除: destroy-all Stop and delete all agent-vm VMs |
| 1077 | | −删除: list List all agent-vm VMs |
| 1078 | | −删除: status Show status of all VMs (current dir marked with >) |
| 1079 | | −删除: name [dir] Print the VM name for a directory (default: cwd) |
| 1080 | | −删除: info [dir] Print machine-readable state as key=value lines |
| 1081 | | −删除: (version, template, state_dir, dir, vm_name, |
| 1082 | | −删除: base_exists, vm_exists, vm_running, vm_stale). |
| 1083 | | −删除: Use this from scripts instead of parsing the output |
| 1084 | | −删除: of the human-facing commands. |
| 1085 | | −删除: env <sub> [args] Read/write ~/.agent-vm/env, the secrets pushed into every |
| 1086 | | −删除: VM. Subcommands: set KEY VALUE, get KEY, has KEY (exit |
| 1087 | | −删除: status only), unset KEY, list (key names, never values). |
| 1088 | | −删除: Use this rather than editing the file: it is sourced by a |
| 1089 | | −删除: shell, so one bad quote costs every secret in it. |
| 1090 | | −删除: version Print the agent-vm version |
| 1091 | | −删除: help Show this help |
| 1092 | | −删除: |
| 1093 | | −删除:VM options (for claude, opencode, codex, vibe, shell, run): |
| 1094 | | −删除: --disk GB VM disk size (default: 10) |
| 1095 | | −删除: --memory GB VM memory (default: 3) |
| 1096 | | −删除: --cpus N Number of CPUs (default: 1) |
| 1097 | | −删除: --reset Destroy and re-clone the VM from the base template |
| 1098 | | −删除: --offline Block outbound internet (keeps host/VM communication) |
| 1099 | | −删除: --readonly Mount the project directory as read-only |
| 1100 | | −删除: --git-read-only Mount .git directory as read-only (allows git diff/log but not commit/stash) |
| 1101 | | −删除: --rm Automatically destroy the VM after the command exits |
| 1102 | | −删除: |
| 1103 | | −删除:Examples: |
| 1104 | | −删除: agent-vm setup # Create base VM |
| 1105 | | −删除: agent-vm claude # Run Claude in a VM |
| 1106 | | −删除: agent-vm opencode # Run OpenCode in a VM |
| 1107 | | −删除: agent-vm codex # Run Codex in a VM |
| 1108 | | −删除: agent-vm vibe # Run Mistral Vibe in a VM |
| 1109 | | −删除: agent-vm --disk 50 --memory 16 --cpus 8 claude # Custom resources |
| 1110 | | −删除: agent-vm --reset claude # Fresh VM from base template |
| 1111 | | −删除: agent-vm --rm claude # Destroy VM after Claude exits |
| 1112 | | −删除: agent-vm --offline claude # No internet access |
| 1113 | | −删除: agent-vm --readonly shell # Read-only project mount |
| 1114 | | −删除: agent-vm --git-ro claude # Protect .git from writes |
| 1115 | | −删除: agent-vm shell # Shell into the VM |
| 1116 | | −删除: agent-vm sh -c "ls -la | grep config" # One-shot command via login zsh |
| 1117 | | −删除: agent-vm run npm install # Run a command in the VM |
| 1118 | | −删除: agent-vm run --tty opencode -p "..." # Run a TUI with PTY allocated |
| 1119 | | −删除: agent-vm claude -p "fix lint errors" # Pass args to claude |
| 1120 | | −删除: |
| 1121 | | −删除:VMs are persistent and unique per directory. Running "agent-vm shell" or |
| 1122 | | −删除:"agent-vm claude" in the same directory will reuse the same VM. |
| 1123 | | −删除: |
| 1124 | | −删除:Customization: |
| 1125 | | −删除: ~/.agent-vm/env Shared env vars / tokens (dotenv-style; |
| 1126 | | −删除: auto-loaded into every VM shell) |
| 1127 | | −删除: ~/.agent-vm/volumes Extra host paths to mount in VMs (one per |
| 1128 | | −删除: line, supports both directories and files) |
| 1129 | | −删除: ~/.agent-vm/setup.sh Per-user setup (runs during "agent-vm setup") |
| 1130 | | −删除: ~/.agent-vm/runtime.sh Per-user runtime (runs on each VM start) |
| 1131 | | −删除: <project>/.agent-vm.runtime.sh Per-project runtime (runs on each VM start) |
| 1132 | | −删除: |
| 1133 | | −删除:More info: https://github.com/sylvinus/agent-vm |
| 1134 | | −删除:EOF |
| 1135 | | −删除:} |
| 1136 | | −删除: |
| 1137 | | −删除:_agent_vm_setup() { |
| 1138 | | −删除: local disk=10 |
| 1139 | | −删除: local memory=3 |
| 1140 | | −删除: local cpus=1 |
| 1141 | | −删除: local preinstall="" |
| 1142 | | −删除: local preinstall_seen="" |
| 1143 | | −删除: # Defaults match the "default install" set: every component on EXCEPT the |
| 1144 | | −删除: # opt-in languages (Ruby, Rust, Go). These apply when --preinstall isn't |
| 1145 | | −删除: # passed and either the wizard's first prompt is accepted or stdin is not a |
| 1146 | | −删除: # terminal (e.g. CI). `--preinstall=all` turns everything on; |
| 1147 | | −删除: # `--preinstall=default,rust` composes the default set with an opt-in. |
| 1148 | | −删除: local install_python=1 install_node=1 |
| 1149 | | −删除: local install_ruby=0 install_rust=0 install_golang=0 |
| 1150 | | −删除: local install_docker=1 install_chromium=1 install_gh=1 |
| 1151 | | −删除: local install_claude=1 install_opencode=1 install_codex=1 install_vibe=1 |
| 1152 | | −删除: # MCP servers wired into the agents' configs. Named mcp-* in --preinstall so |
| 1153 | | −删除: # future MCP servers share one obvious namespace. Only servers with a |
| 1154 | | −删除: # dependency worth baking into the image belong here: a remote MCP server is |
| 1155 | | −删除: # a URL (and often a secret), which belongs in per-project config rather than |
| 1156 | | −删除: # in an image every VM is cloned from. Both current ones drive the installed |
| 1157 | | −删除: # Chromium. mcp-playwright is opt-in like the Ruby/Rust/Go languages: a second |
| 1158 | | −删除: # browser-driving server is redundant for most users, and every wired server |
| 1159 | | −删除: # costs tool definitions in the agent's context. |
| 1160 | | −删除: local install_mcp_chrome=1 install_mcp_playwright=0 |
| 1161 | | −删除: |
| 1162 | | −删除: while [[ $# -gt 0 ]]; do |
| 1163 | | −删除: case "$1" in |
| 1164 | | −删除: --help|-h) |
| 1165 | | −删除: cat << 'EOF' |
| 1166 | | −删除:Usage: agent-vm setup [options] |
| 1167 | | −删除: |
| 1168 | | −删除:Create a base VM template with dev tools and agents pre-installed. Runs an |
| 1169 | | −删除:interactive wizard by default; the first prompt offers a "default install" |
| 1170 | | −删除:(everything except the opt-in languages Ruby, Rust, Go). Answer 'n' for |
| 1171 | | −删除:per-component prompts. Pass --preinstall=... to skip the wizard and pick a |
| 1172 | | −删除:specific subset non-interactively. When stdin is not a terminal (e.g. CI), |
| 1173 | | −删除:the wizard is skipped automatically and the default set is installed. |
| 1174 | | −删除: |
| 1175 | | −删除:Options: |
| 1176 | | −删除: --disk GB VM disk size (default: 10) |
| 1177 | | −删除: --memory GB VM memory (default: 3) |
| 1178 | | −删除: --cpus N Number of CPUs (default: 1) |
| 1179 | | −删除: --preinstall=LIST Comma-separated list of tools to preinstall in the base |
| 1180 | | −删除: VM image (skips the wizard). Anything not listed is |
| 1181 | | −删除: skipped. Use: |
| 1182 | | −删除: 'default' for the default set |
| 1183 | | −删除: (everything except Ruby, Rust, Go), |
| 1184 | | −删除: 'all' for everything, |
| 1185 | | −删除: 'none' for nothing. |
| 1186 | | −删除: Available names: |
| 1187 | | −删除: python, node, ruby, rust, golang, docker, chromium, |
| 1188 | | −删除: gh, claude, opencode, codex, vibe, mcp-chrome, |
| 1189 | | −删除: mcp-playwright |
| 1190 | | −删除: Selecting codex, or chromium with any AI agent, also |
| 1191 | | −删除: installs node because those paths require npm/npx. |
| 1192 | | −删除: The mcp-* names wire an MCP server into each installed |
| 1193 | | −删除: agent's config. Both 'mcp-chrome' (Chrome DevTools) and |
| 1194 | | −删除: 'mcp-playwright' drive the preinstalled Chromium, so |
| 1195 | | −删除: both need node and chromium and are skipped, with a |
| 1196 | | −删除: notice, without them. 'mcp-playwright' is opt-in and not |
| 1197 | | −删除: part of 'default' — a second browser-driving server is |
| 1198 | | −删除: redundant for most users. Omit them to leave the agents' |
| 1199 | | −删除: MCP config untouched — useful when MCP servers are |
| 1200 | | −删除: managed per project rather than baked into the image. |
| 1201 | | −删除: Examples: |
| 1202 | | −删除: --preinstall=default,rust # default set plus Rust |
| 1203 | | −删除: --preinstall=python,docker,claude |
| 1204 | | −删除: --preinstall=node,chromium,opencode # no chrome MCP |
| 1205 | | −删除: --help Show this help |
| 1206 | | −删除:EOF |
| 1207 | | −删除: return 0 |
| 1208 | | −删除: ;; |
| 1209 | | −删除: --disk) |
| 1210 | | −删除: disk="$2" |
| 1211 | | −删除: shift 2 |
| 1212 | | −删除: _agent_vm_validate_int --disk "$disk" || return 1 |
| 1213 | | −删除: ;; |
| 1214 | | −删除: --disk=*) |
| 1215 | | −删除: disk="${1#*=}" |
| 1216 | | −删除: shift |
| 1217 | | −删除: _agent_vm_validate_int --disk "$disk" || return 1 |
| 1218 | | −删除: ;; |
| 1219 | | −删除: --memory|--ram) |
| 1220 | | −删除: memory="$2" |
| 1221 | | −删除: shift 2 |
| 1222 | | −删除: _agent_vm_validate_int --memory "$memory" || return 1 |
| 1223 | | −删除: ;; |
| 1224 | | −删除: --memory=*|--ram=*) |
| 1225 | | −删除: memory="${1#*=}" |
| 1226 | | −删除: shift |
| 1227 | | −删除: _agent_vm_validate_int --memory "$memory" || return 1 |
| 1228 | | −删除: ;; |
| 1229 | | −删除: --cpus) |
| 1230 | | −删除: cpus="$2" |
| 1231 | | −删除: shift 2 |
| 1232 | | −删除: _agent_vm_validate_int --cpus "$cpus" || return 1 |
| 1233 | | −删除: ;; |
| 1234 | | −删除: --cpus=*) |
| 1235 | | −删除: cpus="${1#*=}" |
| 1236 | | −删除: shift |
| 1237 | | −删除: _agent_vm_validate_int --cpus "$cpus" || return 1 |
| 1238 | | −删除: ;; |
| 1239 | | −删除: --preinstall) |
| 1240 | | −删除: preinstall_seen=1 |
| 1241 | | −删除: # Only consume the next argument as the value when it is an actual |
| 1242 | | −删除: # value, not another option. Bare `--preinstall` falls back to the |
| 1243 | | −删除: # default set (handled below) without swallowing e.g. a following |
| 1244 | | −删除: # `--disk`. |
| 1245 | | −删除: if [[ -n "$2" && "$2" != -* ]]; then |
| 1246 | | −删除: preinstall="$2" |
| 1247 | | −删除: shift 2 |
| 1248 | | −删除: else |
| 1249 | | −删除: shift |
| 1250 | | −删除: fi |
| 1251 | | −删除: ;; |
| 1252 | | −删除: --preinstall=*) |
| 1253 | | −删除: preinstall="${1#*=}" |
| 1254 | | −删除: preinstall_seen=1 |
| 1255 | | −删除: shift |
| 1256 | | −删除: ;; |
| 1257 | | −删除: --reset|--offline|--readonly|--git-read-only|--git-ro) |
| 1258 | | −删除: shift ;; |
| 1259 | | −删除: *) |
| 1260 | | −删除: echo "Unknown option: $1" >&2 |
| 1261 | | −删除: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N] [--preinstall=LIST]" >&2 |
| 1262 | | −删除: return 1 |
| 1263 | | −删除: ;; |
| 1264 | | −删除: esac |
| 1265 | | −删除: done |
| 1266 | | −删除: |
| 1267 | | −删除: # Apply --preinstall=LIST: start with everything OFF and turn on what's |
| 1268 | | −删除: # listed. 'default' / 'all' / 'none' are shortcuts. Setting this also |
| 1269 | | −删除: # bypasses the interactive wizard below. `--preinstall=` with no value |
| 1270 | | −删除: # (or `--preinstall` swallowed by a following flag) falls back to the |
| 1271 | | −删除: # default set — explicitly opting into the wizard's recommended install. |
| 1272 | | −删除: # Pass `--preinstall=none` if you really want nothing. |
| 1273 | | −删除: if [[ -n "$preinstall_seen" ]]; then |
| 1274 | | −删除: install_python=0 install_node=0 install_ruby=0 install_rust=0 install_golang=0 |
| 1275 | | −删除: install_docker=0 install_chromium=0 install_gh=0 |
| 1276 | | −删除: install_claude=0 install_opencode=0 install_codex=0 install_vibe=0 |
| 1277 | | −删除: install_mcp_chrome=0 install_mcp_playwright=0 |
| 1278 | | −删除: [[ -z "$preinstall" ]] && preinstall="default" |
| 1279 | | −删除: # Iterate the comma-list portably across bash and zsh by appending a |
| 1280 | | −删除: # trailing comma and peeling off one token per iteration. |
| 1281 | | −删除: local rest="${preinstall}," f |
| 1282 | | −删除: while [[ -n "$rest" ]]; do |
| 1283 | | −删除: f="${rest%%,*}" |
| 1284 | | −删除: rest="${rest#*,}" |
| 1285 | | −删除: # Trim whitespace. Use bash/zsh-portable substitutions only. |
| 1286 | | −删除: f="${f# }"; f="${f% }" |
| 1287 | | −删除: [[ -z "$f" ]] && continue |
| 1288 | | −删除: case "$f" in |
| 1289 | | −删除: all) |
| 1290 | | −删除: install_python=1 install_node=1 install_ruby=1 |
| 1291 | | −删除: install_rust=1 install_golang=1 |
| 1292 | | −删除: install_docker=1 install_chromium=1 install_gh=1 |
| 1293 | | −删除: install_claude=1 install_opencode=1 install_codex=1 install_vibe=1 |
| 1294 | | −删除: install_mcp_chrome=1 install_mcp_playwright=1 |
| 1295 | | −删除: ;; |
| 1296 | | −删除: default) |
| 1297 | | −删除: install_python=1 install_node=1 |
| 1298 | | −删除: install_docker=1 install_chromium=1 install_gh=1 |
| 1299 | | −删除: install_claude=1 install_opencode=1 install_codex=1 install_vibe=1 |
| 1300 | | −删除: install_mcp_chrome=1 |
| 1301 | | −删除: ;; |
| 1302 | | −删除: none) ;; # explicit no-op token; with the all-off reset above, |
| 1303 | | −删除: # `--preinstall=none` ships nothing. |
| 1304 | | −删除: python) install_python=1 ;; |
| 1305 | | −删除: node) install_node=1 ;; |
| 1306 | | −删除: ruby) install_ruby=1 ;; |
| 1307 | | −删除: rust) install_rust=1 ;; |
| 1308 | | −删除: golang) install_golang=1 ;; |
| 1309 | | −删除: docker) install_docker=1 ;; |
| 1310 | | −删除: chromium) install_chromium=1 ;; |
| 1311 | | −删除: gh) install_gh=1 ;; |
| 1312 | | −删除: claude) install_claude=1 ;; |
| 1313 | | −删除: opencode) install_opencode=1 ;; |
| 1314 | | −删除: codex) install_codex=1 ;; |
| 1315 | | −删除: vibe) install_vibe=1 ;; |
| 1316 | | −删除: mcp-chrome) install_mcp_chrome=1 ;; |
| 1317 | | −删除: mcp-playwright) install_mcp_playwright=1 ;; |
| 1318 | | −删除: *) |
| 1319 | | −删除: echo "Unknown preinstall name: $f (names are lowercase)" >&2 |
| 1320 | | −删除: echo "Valid: python, node, ruby, rust, golang, docker, chromium, gh, claude, opencode, codex, vibe, mcp-chrome, mcp-playwright, default, all, none" >&2 |
| 1321 | | −删除: return 1 |
| 1322 | | −删除: ;; |
| 1323 | | −删除: esac |
| 1324 | | −删除: done |
| 1325 | | −删除: fi |
| 1326 | | −删除: |
| 1327 | | −删除: # Fail-fast checks before the (potentially long) wizard so the user doesn't |
| 1328 | | −删除: # answer 15 prompts only to be told their host is missing Lima or KVM. |
| 1329 | | −删除: if ! command -v limactl &>/dev/null; then |
| 1330 | | −删除: if command -v brew &>/dev/null; then |
| 1331 | | −删除: echo "Installing Lima..." |
| 1332 | | −删除: brew install lima |
| 1333 | | −删除: else |
| 1334 | | −删除: echo "Error: Lima is required. Install from https://lima-vm.io/docs/installation/" >&2 |
| 1335 | | −删除: return 1 |
| 1336 | | −删除: fi |
| 1337 | | −删除: fi |
| 1338 | | −删除: |
| 1339 | | −删除: _agent_vm_check_linux_prereqs || return 1 |
| 1340 | | −删除: |
| 1341 | | −删除: # Interactive wizard, unless --preinstall was passed or no terminal is |
| 1342 | | −删除: # attached (e.g. running under CI). Defaults shown in [] are prefilled from |
| 1343 | | −删除: # any --disk/--memory/--cpus flags the user already passed, so they can |
| 1344 | | −删除: # confirm or override. Components default to the "default install" set |
| 1345 | | −删除: # (everything except Ruby/Rust/Go). The first prompt offers that whole set |
| 1346 | | −删除: # as a one-tap shortcut — answer 'n' for per-component prompts. |
| 1347 | | −删除: if [[ -z "$preinstall_seen" ]] && [[ -r /dev/tty ]]; then |
| 1348 | | −删除: printf '\nagent-vm setup wizard\n' >&2 |
| 1349 | | −删除: printf '─────────────────────\n\n' >&2 |
| 1350 | | −删除: printf 'These settings apply to the base VM image. Every per-project VM is\n' >&2 |
| 1351 | | −删除: printf 'cloned from it, so anything preinstalled here is available in all\n' >&2 |
| 1352 | | −删除: printf 'future agent VMs. You can still install extra tools inside any\n' >&2 |
| 1353 | | −删除: printf 'individual VM later (e.g. via `agent-vm shell`).\n\n' >&2 |
| 1354 | | −删除: printf 'For more: https://github.com/sylvinus/agent-vm\n\n' >&2 |
| 1355 | | −删除: |
| 1356 | | −删除: # Software first — the more interesting choice for most users. |
| 1357 | | −删除: printf 'Software\n' >&2 |
| 1358 | | −删除: printf '────────\n' >&2 |
| 1359 | | −删除: printf ' Install: Python, Node.js, Docker, Chromium, gh,\n' >&2 |
| 1360 | | −删除: printf ' Claude Code, OpenCode, Codex CLI, Mistral Vibe,\n' >&2 |
| 1361 | | −删除: printf ' Chrome DevTools MCP\n' >&2 |
| 1362 | | −删除: printf ' Skip: Ruby, Rust, Go, Playwright MCP\n\n' >&2 |
| 1363 | | −删除: local use_default_software |
| 1364 | | −删除: use_default_software=$(_agent_vm_ask_yn "Use this default" Y) |
| 1365 | | −删除: if [[ "$use_default_software" != "1" ]]; then |
| 1366 | | −删除: printf '\nAI coding agents\n' >&2 |
| 1367 | | −删除: printf '────────────────\n' >&2 |
| 1368 | | −删除: install_claude=$(_agent_vm_ask_yn "Claude Code" Y) |
| 1369 | | −删除: install_opencode=$(_agent_vm_ask_yn "OpenCode" Y) |
| 1370 | | −删除: install_codex=$(_agent_vm_ask_yn "Codex CLI" Y) |
| 1371 | | −删除: install_vibe=$(_agent_vm_ask_yn "Mistral Vibe" Y) |
| 1372 | | −删除: |
| 1373 | | −删除: printf '\nSystem tools\n' >&2 |
| 1374 | | −删除: printf '────────────\n' >&2 |
| 1375 | | −删除: install_docker=$(_agent_vm_ask_yn "Docker" Y) |
| 1376 | | −删除: install_chromium=$(_agent_vm_ask_yn "Chromium (headless browser)" Y) |
| 1377 | | −删除: install_gh=$(_agent_vm_ask_yn "GitHub CLI (gh)" Y) |
| 1378 | | −删除: |
| 1379 | | −删除: # MCP servers, wired into each installed agent's config. Chrome DevTools |
| 1380 | | −删除: # drives the Chromium above, so it is only worth asking when that is on. |
| 1381 | | −删除: # Playwright brings its own browser download, hence the N default. |
| 1382 | | −删除: if [[ "$install_chromium" == "1" ]]; then |
| 1383 | | −删除: install_mcp_chrome=$(_agent_vm_ask_yn "Chrome DevTools MCP (wired into each agent's config)" Y) |
| 1384 | | −删除: else |
| 1385 | | −删除: install_mcp_chrome=0 |
| 1386 | | −删除: fi |
| 1387 | | −删除: if [[ "$install_chromium" == "1" ]]; then |
| 1388 | | −删除: install_mcp_playwright=$(_agent_vm_ask_yn "Playwright MCP (also drives that Chromium)" N) |
| 1389 | | −删除: else |
| 1390 | | −删除: install_mcp_playwright=0 |
| 1391 | | −删除: fi |
| 1392 | | −删除: |
| 1393 | | −删除: local node_forced_reason="" |
| 1394 | | −删除: if [[ "$install_codex" == "1" ]]; then |
| 1395 | | −删除: node_forced_reason="Codex CLI requires Node.js" |
| 1396 | | −删除: elif [[ "$install_chromium" == "1" && "$install_mcp_chrome" == "1" && ( "$install_claude" == "1" || "$install_opencode" == "1" || "$install_vibe" == "1" ) ]]; then |
| 1397 | | −删除: node_forced_reason="Chrome DevTools MCP uses npx" |
| 1398 | | −删除: fi |
| 1399 | | −删除: |
| 1400 | | −删除: printf '\nLanguages\n' >&2 |
| 1401 | | −删除: printf '─────────\n' >&2 |
| 1402 | | −删除: install_python=$(_agent_vm_ask_yn "Python 3" Y) |
| 1403 | | −删除: if [[ -n "$node_forced_reason" ]]; then |
| 1404 | | −删除: install_node=1 |
| 1405 | | −删除: printf 'Node.js 24: yes (%s)\n' "$node_forced_reason" >&2 |
| 1406 | | −删除: else |
| 1407 | | −删除: install_node=$(_agent_vm_ask_yn "Node.js 24" Y) |
| 1408 | | −删除: fi |
| 1409 | | −删除: install_ruby=$(_agent_vm_ask_yn "Ruby" Y) |
| 1410 | | −删除: install_rust=$(_agent_vm_ask_yn "Rust" Y) |
| 1411 | | −删除: install_golang=$(_agent_vm_ask_yn "Go" Y) |
| 1412 | | −删除: fi |
| 1413 | | −删除: |
| 1414 | | −删除: # Resources second — same pattern, accept-in-one-go shortcut. Current |
| 1415 | | −删除: # values reflect any --disk/--memory/--cpus already passed on the CLI. |
| 1416 | | −删除: # These are starting values: any later `agent-vm` command can resize the |
| 1417 | | −删除: # per-project VM with --disk/--memory/--cpus. |
| 1418 | | −删除: printf '\nDefault resources\n' >&2 |
| 1419 | | −删除: printf '─────────────────\n' >&2 |
| 1420 | | −删除: printf '(per-VM override with --disk / --memory / --cpus on any agent-vm command)\n\n' >&2 |
| 1421 | | −删除: printf ' Disk %s GB\n' "$disk" >&2 |
| 1422 | | −删除: printf ' Memory %s GB\n' "$memory" >&2 |
| 1423 | | −删除: printf ' CPUs %s\n\n' "$cpus" >&2 |
| 1424 | | −删除: local use_default_resources |
| 1425 | | −删除: use_default_resources=$(_agent_vm_ask_yn "Use these defaults" Y) |
| 1426 | | −删除: if [[ "$use_default_resources" != "1" ]]; then |
| 1427 | | −删除: disk=$(_agent_vm_ask_int "Disk size in GB" "$disk") |
| 1428 | | −删除: memory=$(_agent_vm_ask_int "Memory in GB" "$memory") |
| 1429 | | −删除: cpus=$(_agent_vm_ask_int "Number of CPUs" "$cpus") |
| 1430 | | −删除: fi |
| 1431 | | −删除: printf '\n' >&2 |
| 1432 | | −删除: fi |
| 1433 | | −删除: |
| 1434 | | −删除: if [[ "$install_chromium" == "1" && "$install_mcp_chrome" == "1" ]]; then |
| 1435 | | −删除: local wants_chrome_mcp=0 |
| 1436 | | −删除: [[ "$install_claude" == "1" || "$install_opencode" == "1" || "$install_codex" == "1" || "$install_vibe" == "1" ]] && wants_chrome_mcp=1 |
| 1437 | | −删除: if [[ "$wants_chrome_mcp" == "1" && "$install_node" != "1" ]]; then |
| 1438 | | −删除: echo "Enabling Node.js because Chrome DevTools MCP uses npx." >&2 |
| 1439 | | −删除: install_node=1 |
| 1440 | | −删除: fi |
| 1441 | | −删除: fi |
| 1442 | | −删除: |
| 1443 | | −删除: if [[ "$install_codex" == "1" && "$install_node" != "1" ]]; then |
| 1444 | | −删除: echo "Enabling Node.js because Codex CLI requires npm." >&2 |
| 1445 | | −删除: install_node=1 |
| 1446 | | −删除: fi |
| 1447 | | −删除: |
| 1448 | | −删除: _agent_vm_clean_partial_state "$AGENT_VM_TEMPLATE" |
| 1449 | | −删除: |
| 1450 | | −删除: limactl stop "$AGENT_VM_TEMPLATE" &>/dev/null |
| 1451 | | −删除: limactl delete "$AGENT_VM_TEMPLATE" --force &>/dev/null |
| 1452 | | −删除: |
| 1453 | | −删除: echo "Creating base VM..." |
| 1454 | | −删除: local create_args=( |
| 1455 | | −删除: --set '.mounts=[]' |
| 1456 | | −删除: --disk="$disk" |
| 1457 | | −删除: --memory="$memory" |
| 1458 | | −删除: --cpus="$cpus" |
| 1459 | | −删除: --tty=false |
| 1460 | | −删除: ) |
| 1461 | | −删除: local create_log |
| 1462 | | −删除: if ! create_log=$(limactl create --name="$AGENT_VM_TEMPLATE" template:debian-13 "${create_args[@]}" 2>&1); then |
| 1463 | | −删除: echo "Error: Failed to create base VM." >&2 |
| 1464 | | −删除: echo "--- limactl create output ---" >&2 |
| 1465 | | −删除: echo "$create_log" >&2 |
| 1466 | | −删除: return 1 |
| 1467 | | −删除: fi |
| 1468 | | −删除: |
| 1469 | | −删除: _agent_vm_print_resources "$AGENT_VM_TEMPLATE" |
| 1470 | | −删除: |
| 1471 | | −删除: local start_log |
| 1472 | | −删除: if ! start_log=$(limactl start "$AGENT_VM_TEMPLATE" 2>&1); then |
| 1473 | | −删除: echo "Error: Failed to start base VM." >&2 |
| 1474 | | −删除: echo "--- limactl start output ---" >&2 |
| 1475 | | −删除: echo "$start_log" >&2 |
| 1476 | | −删除: echo "Full log: ~/.lima/$AGENT_VM_TEMPLATE/ha.stderr.log" >&2 |
| 1477 | | −删除: return 1 |
| 1478 | | −删除: fi |
| 1479 | | −删除: |
| 1480 | | −删除: # Run the setup script inside the VM. Component selections are passed by |
| 1481 | | −删除: # prepending `export` lines to the script on stdin — keeps the integration |
| 1482 | | −删除: # to one knob (env vars) and avoids quoting headaches with `limactl shell |
| 1483 | | −删除: # env KEY=VAL`. The setup script's defaults for each flag match the host |
| 1484 | | −删除: # wizard's "default install" set (Ruby/Rust/Go off, everything else on), |
| 1485 | | −删除: # so invoking the in-VM script standalone — without these exports — still |
| 1486 | | −删除: # produces the same default install. |
| 1487 | | −删除: echo "Installing packages inside VM..." |
| 1488 | | −删除: if [[ ! -r "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" ]]; then |
| 1489 | | −删除: echo "Error: Setup script not found at ${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" >&2 |
| 1490 | | −删除: return 1 |
| 1491 | | −删除: fi |
| 1492 | | −删除: { |
| 1493 | | −删除: printf 'export AGENT_VM_INSTALL_PYTHON=%s\n' "$install_python" |
| 1494 | | −删除: printf 'export AGENT_VM_INSTALL_NODE=%s\n' "$install_node" |
| 1495 | | −删除: printf 'export AGENT_VM_INSTALL_RUBY=%s\n' "$install_ruby" |
| 1496 | | −删除: printf 'export AGENT_VM_INSTALL_RUST=%s\n' "$install_rust" |
| 1497 | | −删除: printf 'export AGENT_VM_INSTALL_GOLANG=%s\n' "$install_golang" |
| 1498 | | −删除: printf 'export AGENT_VM_INSTALL_DOCKER=%s\n' "$install_docker" |
| 1499 | | −删除: printf 'export AGENT_VM_INSTALL_CHROMIUM=%s\n' "$install_chromium" |
| 1500 | | −删除: printf 'export AGENT_VM_INSTALL_GH=%s\n' "$install_gh" |
| 1501 | | −删除: printf 'export AGENT_VM_INSTALL_CLAUDE=%s\n' "$install_claude" |
| 1502 | | −删除: printf 'export AGENT_VM_INSTALL_OPENCODE=%s\n' "$install_opencode" |
| 1503 | | −删除: printf 'export AGENT_VM_INSTALL_CODEX=%s\n' "$install_codex" |
| 1504 | | −删除: printf 'export AGENT_VM_INSTALL_VIBE=%s\n' "$install_vibe" |
| 1505 | | −删除: printf 'export AGENT_VM_INSTALL_MCP_CHROME=%s\n' "$install_mcp_chrome" |
| 1506 | | −删除: printf 'export AGENT_VM_INSTALL_MCP_PLAYWRIGHT=%s\n' "$install_mcp_playwright" |
| 1507 | | −删除: cat "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" |
| 1508 | | −删除: } | limactl shell "$AGENT_VM_TEMPLATE" bash -l || { echo "Error: Setup script failed." >&2; return 1; } |
| 1509 | | −删除: |
| 1510 | | −删除: # Run user's custom setup script if it exists |
| 1511 | | −删除: local user_setup="$AGENT_VM_STATE_DIR/setup.sh" |
| 1512 | | −删除: if [ -f "$user_setup" ]; then |
| 1513 | | −删除: echo "Running custom setup from $user_setup..." |
| 1514 | | −删除: limactl shell "$AGENT_VM_TEMPLATE" zsh -l < "$user_setup" || { echo "Error: Custom setup script failed." >&2; return 1; } |
| 1515 | | −删除: fi |
| 1516 | | −删除: |
| 1517 | | −删除: limactl stop "$AGENT_VM_TEMPLATE" &>/dev/null |
| 1518 | | −删除: |
| 1519 | | −删除: # Record base VM version so we can warn about stale clones |
| 1520 | | −删除: mkdir -p "$AGENT_VM_STATE_DIR" |
| 1521 | | −删除: date +%s > "$AGENT_VM_STATE_DIR/.agent-vm-base-version" |
| 1522 | | −删除: |
| 1523 | | −删除: echo "" |
| 1524 | | −删除: echo "Base VM ready. Try one of these in any project directory:" |
| 1525 | | −删除: echo " agent-vm shell" |
| 1526 | | −删除: [[ "$install_claude" == "1" ]] && echo " agent-vm claude" |
| 1527 | | −删除: [[ "$install_opencode" == "1" ]] && echo " agent-vm opencode" |
| 1528 | | −删除: [[ "$install_codex" == "1" ]] && echo " agent-vm codex" |
| 1529 | | −删除: [[ "$install_vibe" == "1" ]] && echo " agent-vm vibe" |
| 1530 | | −删除: echo "" |
| 1531 | | −删除: echo "Note: Existing VMs were not updated. Use --reset to re-clone them from the new base." |
| 1532 | | −删除:} |
| 800 | +新增:# --- commands ----------------------------------------------------------------- |
| 1533 | 801 | |
| 1534 | 802 | # Run a command in the VM through a login zsh. |
| 1535 | 803 | # |
| @@ -1543,7 +811,9 @@ EOF |
| 1543 | 811 | # |
| 1544 | 812 | # The command and its arguments are passed as positional parameters, so the |
| 1545 | 813 | # guest shell never re-parses them; `env` runs the command and keeps leading |
| 1546 | | −删除:# VAR=value assignments working. |
| 814 | +新增:# VAR=value assignments working. `env --`: GNU env still reads assignments |
| 815 | +新增:# after it, and a command starting with `-` (`run -i foo`) is not taken for |
| 816 | +新增:# one of its options. |
| 1547 | 817 | # |
| 1548 | 818 | # Usage: _agent_vm_lima_run <vm_name> <host_dir> <tty:1|""> <command> [args...] |
| 1549 | 819 | _agent_vm_lima_run() { |
| @@ -1551,345 +821,316 @@ _agent_vm_lima_run() { |
| 1551 | 821 | shift 3 |
| 1552 | 822 | local shell_opts=(--workdir "$host_dir") |
| 1553 | 823 | [[ -n "$want_tty" ]] && shell_opts+=(--tty) |
| 1554 | | −删除: limactl shell "${shell_opts[@]}" "$vm_name" -- zsh -l -c 'exec env "$@"' agent-vm "$@" |
| 824 | +新增: limactl shell "${shell_opts[@]}" "$vm_name" -- zsh -l -c 'exec env -- "$@"' agent-vm "$@" |
| 1555 | 825 | } |
| 1556 | 826 | |
| 1557 | | −删除:_agent_vm_claude() { |
| 1558 | | −删除: local vm_opts=() |
| 1559 | | −删除: local args=() |
| 1560 | | −删除: local rm="" |
| 1561 | | −删除: while [[ $# -gt 0 ]]; do |
| 1562 | | −删除: case "$1" in |
| 1563 | | −删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;; |
| 1564 | | −删除: --memory|--ram) vm_opts+=(--memory "$2"); shift 2 ;; |
| 1565 | | −删除: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;; |
| 1566 | | −删除: --reset) vm_opts+=(--reset); shift ;; |
| 1567 | | −删除: --offline) vm_opts+=(--offline); shift ;; |
| 1568 | | −删除: --readonly) vm_opts+=(--readonly); shift ;; |
| 1569 | | −删除: --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;; |
| 1570 | | −删除: --rm) rm=1; shift ;; |
| 1571 | | −删除: *) args+=("$1"); shift ;; |
| 1572 | | −删除: esac |
| 1573 | | −删除: done |
| 1574 | | −删除: local host_dir |
| 827 | +新增:# Starts this directory's VM with the caller's vm_opts, runs <function> with |
| 828 | +新增:# the VM name and the directory before its own arguments, then deletes the VM |
| 829 | +新增:# when the caller's rm is set. Returns the function's status. With --scratch, |
| 830 | +新增:# see _agent_vm_run_scratch. |
| 831 | +新增:_agent_vm_in_vm() { |
| 832 | +新增: local fn="$1" host_dir vm_name st=0 o |
| 833 | +新增: shift |
| 1575 | 834 | host_dir="$(pwd)" |
| 1576 | | −删除: local vm_name |
| 1577 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" |
| 1578 | | −删除: |
| 1579 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1580 | | −删除: _agent_vm_print_resources "$vm_name" |
| 1581 | | −删除: |
| 1582 | | −删除: local exit_code=0 |
| 1583 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" "" claude --dangerously-skip-permissions ${args[@]+"${args[@]}"} |
| 1584 | | −删除: exit_code=$? |
| 1585 | | −删除: [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1586 | | −删除: return $exit_code |
| 1587 | | −删除:} |
| 1588 | | −删除: |
| 1589 | | −删除:_agent_vm_opencode() { |
| 1590 | | −删除: local vm_opts=() |
| 1591 | | −删除: local args=() |
| 1592 | | −删除: local rm="" |
| 1593 | | −删除: while [[ $# -gt 0 ]]; do |
| 1594 | | −删除: case "$1" in |
| 1595 | | −删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;; |
| 1596 | | −删除: --memory|--ram) vm_opts+=(--memory "$2"); shift 2 ;; |
| 1597 | | −删除: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;; |
| 1598 | | −删除: --reset) vm_opts+=(--reset); shift ;; |
| 1599 | | −删除: --offline) vm_opts+=(--offline); shift ;; |
| 1600 | | −删除: --readonly) vm_opts+=(--readonly); shift ;; |
| 1601 | | −删除: --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;; |
| 1602 | | −删除: --rm) rm=1; shift ;; |
| 1603 | | −删除: *) args+=("$1"); shift ;; |
| 1604 | | −删除: esac |
| 835 | +新增: for o in ${vm_opts[@]+"${vm_opts[@]}"}; do |
| 836 | +新增: if [[ "$o" == --scratch ]]; then |
| 837 | +新增: _agent_vm_run_scratch "$fn" "$host_dir" "$@" |
| 838 | +新增: return |
| 839 | +新增: fi |
| 1605 | 840 | done |
| 1606 | | −删除: local host_dir |
| 1607 | | −删除: host_dir="$(pwd)" |
| 1608 | | −删除: local vm_name |
| 1609 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" |
| 1610 | | −删除: |
| 841 | +新增: vm_name="$(_agent_vm_name "$host_dir")" || return 1 |
| 1611 | 842 | _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1612 | 843 | _agent_vm_print_resources "$vm_name" |
| 1613 | | −删除: |
| 1614 | | −删除: # --auto auto-approves permission prompts that aren't explicitly denied, |
| 1615 | | −删除: # giving full autonomy (safe inside the sandbox). This is OpenCode's shipped |
| 1616 | | −删除: # equivalent of a "yolo" mode; the proposed --yolo flag was never merged. |
| 1617 | | −删除: local exit_code=0 |
| 1618 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" 1 opencode --auto ${args[@]+"${args[@]}"} |
| 1619 | | −删除: exit_code=$? |
| 1620 | | −删除: [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1621 | | −删除: return $exit_code |
| 844 | +新增: "$fn" "$vm_name" "$host_dir" "$@" || st=$? |
| 845 | +新增: _agent_vm_reset_term_modes |
| 846 | +新增: if [[ -n "$rm" ]]; then |
| 847 | +新增: echo "Removing VM..." |
| 848 | +新增: _agent_vm_destroy |
| 849 | +新增: fi |
| 850 | +新增: return "$st" |
| 1622 | 851 | } |
| 1623 | 852 | |
| 1624 | | −删除:_agent_vm_codex() { |
| 1625 | | −删除: local vm_opts=() |
| 1626 | | −删除: local args=() |
| 1627 | | −删除: local rm="" |
| 1628 | | −删除: while [[ $# -gt 0 ]]; do |
| 1629 | | −删除: case "$1" in |
| 1630 | | −删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;; |
| 1631 | | −删除: --memory|--ram) vm_opts+=(--memory "$2"); shift 2 ;; |
| 1632 | | −删除: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;; |
| 1633 | | −删除: --reset) vm_opts+=(--reset); shift ;; |
| 1634 | | −删除: --offline) vm_opts+=(--offline); shift ;; |
| 1635 | | −删除: --readonly) vm_opts+=(--readonly); shift ;; |
| 1636 | | −删除: --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;; |
| 1637 | | −删除: --rm) rm=1; shift ;; |
| 1638 | | −删除: *) args+=("$1"); shift ;; |
| 1639 | | −删除: esac |
| 1640 | | −删除: done |
| 1641 | | −删除: local host_dir |
| 1642 | | −删除: host_dir="$(pwd)" |
| 1643 | | −删除: local vm_name |
| 1644 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" |
| 1645 | | −删除: |
| 1646 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1647 | | −删除: _agent_vm_print_resources "$vm_name" |
| 1648 | | −删除: |
| 1649 | | −删除: local exit_code=0 |
| 1650 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" "" codex --dangerously-bypass-approvals-and-sandbox ${args[@]+"${args[@]}"} |
| 1651 | | −删除: exit_code=$? |
| 1652 | | −删除: [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1653 | | −删除: return $exit_code |
| 853 | +新增:# --scratch: a new VM of its own (see _agent_vm_scratch_name), sharing |
| 854 | +新增:# nothing, deleted once <function> returns, whatever it returns, unless the |
| 855 | +新增:# user keeps it a while to look inside (asked below). Ctrl-C |
| 856 | +新增:# included: the trap lets the run go on to the deletion instead of ending |
| 857 | +新增:# the shell with the interrupted command (bash ends a script whose child dies |
| 858 | +新增:# of SIGINT, unless it traps it). A run that is killed outright leaves its VM |
| 859 | +新增:# recorded with its pid, and the next --scratch run deletes it. |
| 860 | +新增:_agent_vm_run_scratch() { |
| 861 | +新增: local fn="$1" host_dir="$2" vm_name st=0 old_int="" leftover |
| 862 | +新增: shift 2 |
| 863 | +新增: while IFS= read -r leftover; do |
| 864 | +新增: [[ -n "$leftover" ]] || continue |
| 865 | +新增: echo "Deleting scratch VM '$leftover', left by a run that did not finish..." |
| 866 | +新增: _agent_vm_delete_vm "$leftover" |
| 867 | +新增: done <<< "$(_agent_vm_scratch_leftovers)" |
| 868 | +新增: vm_name="$(_agent_vm_scratch_name "$host_dir")" || return 1 |
| 869 | +新增: mkdir -p "$AGENT_VM_STATE_DIR" && printf '%s\n' "$$" > "$(_agent_vm_scratch_marker "$vm_name")" || return 1 |
| 870 | +新增: # The caller's: the shell session says the VM goes on exit. |
| 871 | +新增: rm=1 |
| 872 | +新增: old_int="$(trap -p INT)" |
| 873 | +新增: trap ':' INT |
| 874 | +新增: if _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"}; then |
| 875 | +新增: _agent_vm_print_resources "$vm_name" |
| 876 | +新增: "$fn" "$vm_name" "$host_dir" "$@" || st=$? |
| 877 | +新增: _agent_vm_reset_term_modes |
| 878 | +新增: # Asked before the deletion, when it can be (yes by default): a no opens |
| 879 | +新增: # a shell in the VM, to look at what the command left, and asks again on |
| 880 | +新增: # its exit. Only an explicit no keeps it: no terminal, or a question cut |
| 881 | +新增: # short, deletes. |
| 882 | +新增: while _agent_vm_can_ask \ |
| 883 | +新增: && [[ "$(_agent_vm_ask_yn "Delete scratch VM '$vm_name'?" Y)" == "0" ]]; do |
| 884 | +新增: echo "A shell in scratch VM '$vm_name'. Type 'exit' to be asked again." |
| 885 | +新增: limactl shell --workdir "$host_dir" "$vm_name" zsh -l |
| 886 | +新增: _agent_vm_reset_term_modes |
| 887 | +新增: done |
| 888 | +新增: else |
| 889 | +新增: st=1 |
| 890 | +新增: fi |
| 891 | +新增: echo "Deleting scratch VM '$vm_name'..." |
| 892 | +新增: _agent_vm_delete_vm "$vm_name" || st=1 |
| 893 | +新增: trap - INT |
| 894 | +新增: [[ -z "$old_int" ]] || eval "$old_int" |
| 895 | +新增: return "$st" |
| 1654 | 896 | } |
| 1655 | 897 | |
| 1656 | | −删除:_agent_vm_vibe() { |
| 1657 | | −删除: local vm_opts=() |
| 1658 | | −删除: local args=() |
| 1659 | | −删除: local rm="" |
| 1660 | | −删除: while [[ $# -gt 0 ]]; do |
| 1661 | | −删除: case "$1" in |
| 1662 | | −删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;; |
| 1663 | | −删除: --memory|--ram) vm_opts+=(--memory "$2"); shift 2 ;; |
| 1664 | | −删除: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;; |
| 1665 | | −删除: --reset) vm_opts+=(--reset); shift ;; |
| 1666 | | −删除: --offline) vm_opts+=(--offline); shift ;; |
| 1667 | | −删除: --readonly) vm_opts+=(--readonly); shift ;; |
| 1668 | | −删除: --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;; |
| 1669 | | −删除: --rm) rm=1; shift ;; |
| 1670 | | −删除: *) args+=("$1"); shift ;; |
| 1671 | | −删除: esac |
| 1672 | | −删除: done |
| 1673 | | −删除: local host_dir |
| 1674 | | −删除: host_dir="$(pwd)" |
| 1675 | | −删除: local vm_name |
| 1676 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" |
| 1677 | | −删除: |
| 1678 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1679 | | −删除: _agent_vm_print_resources "$vm_name" |
| 1680 | | −删除: |
| 1681 | | −删除: # Vibe is a full-screen TUI, so allocate a tty (like opencode). |
| 1682 | | −删除: # --agent auto-approve gives full autonomy (safe inside the sandbox). |
| 1683 | | −删除: local exit_code=0 |
| 1684 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" 1 vibe --agent auto-approve ${args[@]+"${args[@]}"} |
| 1685 | | −删除: exit_code=$? |
| 1686 | | −删除: [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1687 | | −删除: return $exit_code |
| 898 | +新增:# `agent-vm <agent> [options] [args]`: the agent, with the flag that lets it |
| 899 | +新增:# work unattended (safe in the VM), and a TTY for the full-screen ones. |
| 900 | +新增:_agent_vm_agent() { |
| 901 | +新增: local agent="$1" cmd=() tty="" vm_opts=() rm="" args=() |
| 902 | +新增: shift |
| 903 | +新增: case "$agent" in |
| 904 | +新增: # The VM also enforces bypass mode with managed settings (see |
| 905 | +新增: # agent-vm.setup.sh): Claude Code drops the flag when it relaunches itself. |
| 906 | +新增: claude) cmd=(claude --dangerously-skip-permissions) ;; |
| 907 | +新增: # Approves every prompt that is not explicitly denied: OpenCode's shipped |
| 908 | +新增: # "yolo" (a --yolo flag was proposed, never merged). |
| 909 | +新增: opencode) cmd=(opencode --auto); tty=1 ;; |
| 910 | +新增: codex) cmd=(codex --dangerously-bypass-approvals-and-sandbox) ;; |
| 911 | +新增: vibe) cmd=(vibe --agent auto-approve); tty=1 ;; |
| 912 | +新增: # No permission prompts: it runs every tool as asked. |
| 913 | +新增: pi) cmd=(pi); tty=1 ;; |
| 914 | +新增: *) echo "Error: unknown agent: $agent" >&2; return 1 ;; |
| 915 | +新增: esac |
| 916 | +新增: _agent_vm_split_args "$@" || return 1 |
| 917 | +新增: _agent_vm_in_vm _agent_vm_lima_run "$tty" "${cmd[@]}" ${args[@]+"${args[@]}"} |
| 1688 | 918 | } |
| 1689 | 919 | |
| 920 | +新增:# `agent-vm shell [-c "command"]`: a login zsh in the VM, so ~/.zshenv (mise, |
| 921 | +新增:# PATH, ~/.agent-vm.env) is loaded. Takes no command of its own, so a word it |
| 922 | +新增:# does not know is an error, not something skipped as if it had applied. |
| 1690 | 923 | _agent_vm_shell() { |
| 1691 | | −删除: local vm_opts=() |
| 1692 | | −删除: local rm="" |
| 1693 | | −删除: local cmd_string="" |
| 924 | +新增: local vm_opts=() rm="" cmd_string="" taken |
| 1694 | 925 | while [[ $# -gt 0 ]]; do |
| 926 | +新增: _agent_vm_take_opt "$@" || return 1 |
| 927 | +新增: if [[ $taken -gt 0 ]]; then |
| 928 | +新增: shift "$taken" |
| 929 | +新增: continue |
| 930 | +新增: fi |
| 1695 | 931 | case "$1" in |
| 1696 | | −删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;; |
| 1697 | | −删除: --memory|--ram) vm_opts+=(--memory "$2"); shift 2 ;; |
| 1698 | | −删除: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;; |
| 1699 | | −删除: --reset) vm_opts+=(--reset); shift ;; |
| 1700 | | −删除: --offline) vm_opts+=(--offline); shift ;; |
| 1701 | | −删除: --readonly) vm_opts+=(--readonly); shift ;; |
| 1702 | | −删除: --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;; |
| 1703 | | −删除: --rm) rm=1; shift ;; |
| 1704 | 932 | -c|--command) |
| 1705 | 933 | if [[ $# -lt 2 || -z "$2" ]]; then |
| 1706 | 934 | echo "Error: -c/--command requires a command string." >&2 |
| 1707 | 935 | return 1 |
| 1708 | 936 | fi |
| 1709 | 937 | cmd_string="$2"; shift 2 ;; |
| 1710 | | −删除: *) shift ;; |
| 938 | +新增: *) |
| 939 | +新增: echo "Error: unknown argument for shell: $1" >&2 |
| 940 | +新增: echo "Usage: agent-vm [vm options] shell [-c \"command\"]" >&2 |
| 941 | +新增: return 1 ;; |
| 1711 | 942 | esac |
| 1712 | 943 | done |
| 1713 | | −删除: local host_dir |
| 1714 | | −删除: host_dir="$(pwd)" |
| 1715 | | −删除: local vm_name |
| 1716 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" |
| 1717 | | −删除: |
| 1718 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1719 | | −删除: _agent_vm_print_resources "$vm_name" |
| 944 | +新增: _agent_vm_in_vm _agent_vm_shell_session "$cmd_string" |
| 945 | +新增:} |
| 1720 | 946 | |
| 1721 | | −删除: local exit_code=0 |
| 947 | +新增:_agent_vm_shell_session() { |
| 948 | +新增: local vm_name="$1" host_dir="$2" cmd_string="$3" |
| 1722 | 949 | if [[ -n "$cmd_string" ]]; then |
| 1723 | | −删除: # One-shot: run the command via a login zsh so ~/.zshenv (mise, PATH, |
| 1724 | | −删除: # ~/.agent-vm.env, …) is loaded. No "Type 'exit'..." chatter. |
| 1725 | 950 | limactl shell --workdir "$host_dir" "$vm_name" zsh -l -c "$cmd_string" |
| 1726 | | −删除: exit_code=$? |
| 951 | +新增: return |
| 952 | +新增: fi |
| 953 | +新增: echo "VM: $vm_name | Dir: $host_dir" |
| 954 | +新增: if [[ -n "$rm" ]]; then |
| 955 | +新增: echo "Type 'exit' to leave. VM will be destroyed after exit." |
| 1727 | 956 | else |
| 1728 | | −删除: echo "VM: $vm_name | Dir: $host_dir" |
| 1729 | | −删除: if [[ -n "$rm" ]]; then |
| 1730 | | −删除: echo "Type 'exit' to leave. VM will be destroyed after exit." |
| 1731 | | −删除: else |
| 1732 | | −删除: echo "Type 'exit' to leave (VM keeps running). Use 'agent-vm stop' to stop it." |
| 1733 | | −删除: fi |
| 1734 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" zsh -l |
| 1735 | | −删除: exit_code=$? |
| 957 | +新增: echo "Type 'exit' to leave (VM keeps running). Use 'agent-vm stop' to stop it." |
| 1736 | 958 | fi |
| 1737 | | −删除: [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1738 | | −删除: return $exit_code |
| 959 | +新增: limactl shell --workdir "$host_dir" "$vm_name" zsh -l |
| 1739 | 960 | } |
| 1740 | 961 | |
| 962 | +新增:# `agent-vm run [--tty] <command> [args]`. --tty gives the command a |
| 963 | +新增:# pseudo-terminal, which full-screen TUIs (htop, …) need to draw. |
| 1741 | 964 | _agent_vm_run() { |
| 1742 | | −删除: local vm_opts=() |
| 1743 | | −删除: local args=() |
| 1744 | | −删除: local rm="" |
| 1745 | | −删除: local tty_flag="" |
| 1746 | | −删除: while [[ $# -gt 0 ]]; do |
| 1747 | | −删除: case "$1" in |
| 1748 | | −删除: --disk) vm_opts+=(--disk "$2"); shift 2 ;; |
| 1749 | | −删除: --memory|--ram) vm_opts+=(--memory "$2"); shift 2 ;; |
| 1750 | | −删除: --cpus) vm_opts+=(--cpus "$2"); shift 2 ;; |
| 1751 | | −删除: --reset) vm_opts+=(--reset); shift ;; |
| 1752 | | −删除: --offline) vm_opts+=(--offline); shift ;; |
| 1753 | | −删除: --readonly) vm_opts+=(--readonly); shift ;; |
| 1754 | | −删除: --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;; |
| 1755 | | −删除: --rm) rm=1; shift ;; |
| 1756 | | −删除: --tty) tty_flag=1; shift ;; |
| 1757 | | −删除: *) args+=("$1"); shift ;; |
| 1758 | | −删除: esac |
| 1759 | | −删除: done |
| 965 | +新增: local vm_opts=() rm="" tty="" args=() |
| 966 | +新增: _agent_vm_split_args "$@" || return 1 |
| 1760 | 967 | if [[ ${#args[@]} -eq 0 ]]; then |
| 1761 | 968 | echo "Usage: agent-vm run [--tty] <command> [args]" >&2 |
| 1762 | 969 | return 1 |
| 1763 | 970 | fi |
| 1764 | | −删除: local host_dir |
| 1765 | | −删除: host_dir="$(pwd)" |
| 971 | +新增: _agent_vm_in_vm _agent_vm_lima_run "$tty" "${args[@]}" |
| 972 | +新增:} |
| 973 | +新增: |
| 974 | +新增:# Resolve the VM that `stop` / `rm` acts on, and prove it exists. |
| 975 | +新增:# Prints the name on stdout; diagnostics go to stderr. |
| 976 | +新增:# |
| 977 | +新增:# With no argument: the current directory's VM, as before. |
| 978 | +新增:# |
| 979 | +新增:# With one argument: a VM name as printed by `agent-vm list`. It has to be a |
| 980 | +新增:# NAME and not a directory, because the case it exists for is a VM no directory |
| 981 | +新增:# can reach any more: the name embeds a hash of the path, so renaming or |
| 982 | +新增:# deleting the project folder orphans the VM, and `_agent_vm_abs_dir` would |
| 983 | +新增:# refuse the old path anyway. `list` remains the only handle on it. |
| 984 | +新增:# |
| 985 | +新增:# The `agent-vm-` prefix is required: without it a typo could stop or delete an |
| 986 | +新增:# unrelated Lima instance on the same machine. |
| 987 | +新增:_agent_vm_resolve_target() { |
| 988 | +新增: local verb="$1" |
| 989 | +新增: shift |
| 990 | +新增: |
| 1766 | 991 | local vm_name |
| 1767 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" |
| 992 | +新增: case $# in |
| 993 | +新增: 0) vm_name="$(_agent_vm_name "$(pwd)")" || return 1 ;; |
| 994 | +新增: 1) |
| 995 | +新增: vm_name="$1" |
| 996 | +新增: case "$vm_name" in |
| 997 | +新增: agent-vm-*) ;; |
| 998 | +新增: *) |
| 999 | +新增: echo "Error: '$vm_name' is not an agent-vm VM name (they start with 'agent-vm-')." >&2 |
| 1000 | +新增: echo "Run 'agent-vm list' to see them." >&2 |
| 1001 | +新增: return 1 ;; |
| 1002 | +新增: esac ;; |
| 1003 | +新增: *) |
| 1004 | +新增: echo "Usage: agent-vm $verb [vm-name]" >&2 |
| 1005 | +新增: return 1 ;; |
| 1006 | +新增: esac |
| 1768 | 1007 | |
| 1769 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1770 | | −删除: _agent_vm_print_resources "$vm_name" |
| 1008 | +新增: local st=0 |
| 1009 | +新增: _agent_vm_exists "$vm_name" || st=$? |
| 1010 | +新增: case "$st" in |
| 1011 | +新增: 0) ;; |
| 1012 | +新增: 1) |
| 1013 | +新增: if [[ $# -eq 0 ]]; then |
| 1014 | +新增: echo "No VM found for this directory." >&2 |
| 1015 | +新增: echo "Run 'agent-vm list' to see existing VMs, then 'agent-vm $verb <vm-name>'." >&2 |
| 1016 | +新增: else |
| 1017 | +新增: echo "Error: no such VM: $vm_name" >&2 |
| 1018 | +新增: echo "Run 'agent-vm list' to see existing VMs." >&2 |
| 1019 | +新增: fi |
| 1020 | +新增: return 1 ;; |
| 1021 | +新增: *) |
| 1022 | +新增: echo "Error: could not query Lima. Is it installed and working?" >&2 |
| 1023 | +新增: return 1 ;; |
| 1024 | +新增: esac |
| 1771 | 1025 | |
| 1772 | | −删除: # `--tty` forces limactl to allocate a pseudo-terminal in the VM, which |
| 1773 | | −删除: # full-screen TUIs (opencode, vibe, htop, …) need to render correctly when |
| 1774 | | −删除: # invoked through `agent-vm run`. Without it, line-mode tools work fine but |
| 1775 | | −删除: # ncurses-style UIs break. |
| 1776 | | −删除: local exit_code=0 |
| 1777 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" "$tty_flag" "${args[@]}" |
| 1778 | | −删除: exit_code=$? |
| 1779 | | −删除: [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1780 | | −删除: return $exit_code |
| 1026 | +新增: echo "$vm_name" |
| 1781 | 1027 | } |
| 1782 | 1028 | |
| 1783 | 1029 | _agent_vm_stop() { |
| 1784 | | −删除: local host_dir |
| 1785 | | −删除: host_dir="$(pwd)" |
| 1786 | 1030 | local vm_name |
| 1787 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" |
| 1031 | +新增: vm_name="$(_agent_vm_resolve_target stop "$@")" || return 1 |
| 1788 | 1032 | |
| 1789 | | −删除: if ! _agent_vm_exists "$vm_name"; then |
| 1790 | | −删除: echo "No VM found for this directory." >&2 |
| 1033 | +新增: echo "Stopping VM '$vm_name'..." |
| 1034 | +新增: if ! _agent_vm_stop_vm "$vm_name"; then |
| 1035 | +新增: echo "Error: VM '$vm_name' is still running, or Lima cannot say. See 'limactl list'." >&2 |
| 1791 | 1036 | return 1 |
| 1792 | 1037 | fi |
| 1793 | | −删除: |
| 1794 | | −删除: echo "Stopping VM '$vm_name'..." |
| 1795 | | −删除: limactl stop "$vm_name" &>/dev/null |
| 1796 | 1038 | echo "VM stopped." |
| 1797 | 1039 | } |
| 1798 | 1040 | |
| 1799 | 1041 | _agent_vm_destroy() { |
| 1800 | | −删除: local host_dir |
| 1801 | | −删除: host_dir="$(pwd)" |
| 1802 | 1042 | local vm_name |
| 1803 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" |
| 1804 | | −删除: |
| 1805 | | −删除: if ! _agent_vm_exists "$vm_name"; then |
| 1806 | | −删除: echo "No VM found for this directory." >&2 |
| 1807 | | −删除: return 1 |
| 1808 | | −删除: fi |
| 1043 | +新增: vm_name="$(_agent_vm_resolve_target rm "$@")" || return 1 |
| 1809 | 1044 | |
| 1810 | 1045 | echo "Stopping and deleting VM '$vm_name'..." |
| 1811 | | −删除: limactl stop "$vm_name" &>/dev/null |
| 1812 | | −删除: limactl delete "$vm_name" --force &>/dev/null |
| 1813 | | −删除: _agent_vm_cleanup_state "$vm_name" |
| 1046 | +新增: _agent_vm_delete_vm "$vm_name" || return 1 |
| 1814 | 1047 | echo "VM destroyed." |
| 1815 | 1048 | } |
| 1816 | 1049 | |
| 1050 | +新增:# Every agent-vm VM, the base template included: this is the command that |
| 1051 | +新增:# gives the disk space back. `agent-vm setup` rebuilds the template. |
| 1817 | 1052 | _agent_vm_destroy_all() { |
| 1818 | | −删除: local vms |
| 1819 | | −删除: vms=$(limactl list -q 2>/dev/null | grep "^agent-vm-" || true) |
| 1053 | +新增: local list vms |
| 1054 | +新增: if ! list="$(limactl list -q 2>/dev/null)"; then |
| 1055 | +新增: echo "Error: could not query Lima. Is it installed and working?" >&2 |
| 1056 | +新增: return 1 |
| 1057 | +新增: fi |
| 1058 | +新增: vms="$(printf '%s\n' "$list" | grep "^agent-vm-" || true)" |
| 1820 | 1059 | if [[ -z "$vms" ]]; then |
| 1821 | 1060 | echo "No agent-vm VMs found." |
| 1822 | 1061 | return 0 |
| 1823 | 1062 | fi |
| 1824 | 1063 | echo "This will destroy the following VMs:" |
| 1825 | 1064 | echo "$vms" |
| 1826 | | −删除: printf "Continue? [y/N] " >&2 |
| 1827 | | −删除: local reply="" |
| 1828 | | −删除: IFS= read -r reply </dev/tty 2>/dev/null || reply="" |
| 1829 | | −删除: if [[ ! "$reply" =~ ^[Yy]$ ]]; then |
| 1065 | +新增: if _agent_vm_has_line "$vms" "$AGENT_VM_TEMPLATE"; then |
| 1066 | +新增: echo "($AGENT_VM_TEMPLATE is the base template: 'agent-vm setup' rebuilds it.)" |
| 1067 | +新增: fi |
| 1068 | +新增: if ! _agent_vm_can_ask || [[ "$(_agent_vm_ask_yn "Continue?" N)" != "1" ]]; then |
| 1830 | 1069 | echo "Aborted." |
| 1831 | 1070 | return 0 |
| 1832 | 1071 | fi |
| 1833 | | −删除: echo "$vms" | while read -r vm; do |
| 1834 | | −删除: echo "Destroying $vm..." |
| 1835 | | −删除: limactl stop "$vm" &>/dev/null |
| 1836 | | −删除: limactl delete "$vm" --force &>/dev/null |
| 1837 | | −删除: _agent_vm_cleanup_state "$vm" |
| 1838 | | −删除: done |
| 1072 | +新增: _agent_vm_destroy_vms "$vms" || return 1 |
| 1839 | 1073 | echo "All VMs destroyed." |
| 1840 | 1074 | } |
| 1841 | 1075 | |
| 1842 | | −删除:_agent_vm_list() { |
| 1843 | | −删除: limactl list | head -1 |
| 1844 | | −删除: limactl list | grep "^agent-vm-" || echo "(no VMs)" |
| 1076 | +新增:# Stop, delete and forget each VM named on its own line in $1. Goes through |
| 1077 | +新增:# all of them, and fails if one could not be deleted. |
| 1078 | +新增:# limactl gets /dev/null as stdin (see _agent_vm_delete_vm): the names are |
| 1079 | +新增:# read from stdin, and a limactl that reads it would swallow the ones still to |
| 1080 | +新增:# come. |
| 1081 | +新增:_agent_vm_destroy_vms() { |
| 1082 | +新增: local vm st=0 |
| 1083 | +新增: while IFS= read -r vm; do |
| 1084 | +新增: [[ -n "$vm" ]] || continue |
| 1085 | +新增: echo "Destroying $vm..." |
| 1086 | +新增: _agent_vm_delete_vm "$vm" || st=1 |
| 1087 | +新增: done <<< "$1" |
| 1088 | +新增: return "$st" |
| 1845 | 1089 | } |
| 1846 | 1090 | |
| 1847 | | −删除:_agent_vm_status() { |
| 1848 | | −删除: local host_dir |
| 1849 | | −删除: host_dir="$(pwd)" |
| 1850 | | −删除: local current_vm_name |
| 1851 | | −删除: current_vm_name="$(_agent_vm_name "$host_dir")" |
| 1852 | | −删除: |
| 1853 | | −删除: local header |
| 1854 | | −删除: header=$(limactl list | head -1) |
| 1855 | | −删除: |
| 1856 | | −删除: echo "VMs (current directory: $host_dir):" |
| 1857 | | −删除: echo "" |
| 1858 | | −删除: echo "$header" | sed 's/^/ /' |
| 1859 | | −删除: limactl list | grep "^agent-vm-" | while read -r line; do |
| 1860 | | −删除: local vm_name |
| 1861 | | −删除: vm_name=$(echo "$line" | awk '{print $1}') |
| 1862 | | −删除: if [[ "$vm_name" == "$current_vm_name" ]]; then |
| 1863 | | −删除: echo "$line" | sed 's/^/> /' |
| 1864 | | −删除: else |
| 1865 | | −删除: echo "$line" | sed 's/^/ /' |
| 1091 | +新增:# Lima's table of the agent-vm VMs, with the base each was cloned from (see |
| 1092 | +新增:# _agent_vm_base_label) in a last column. Lima pads every column but its last, |
| 1093 | +新增:# so the lines are padded here to line it up. |
| 1094 | +新增:# `list` and `status`: Lima's table of the agent-vm VMs, the current |
| 1095 | +新增:# directory's marked with >, and the base each was cloned from (see |
| 1096 | +新增:# _agent_vm_base_label) in a last column. Lima pads every column but its last, |
| 1097 | +新增:# so the lines are padded here to line it up. |
| 1098 | +新增:_agent_vm_list() { |
| 1099 | +新增: local list line lines=() w=0 header=1 current mark |
| 1100 | +新增: current="$(_agent_vm_name "$(pwd)" 2>/dev/null)" || current="" |
| 1101 | +新增: if ! list="$(limactl list 2>/dev/null)"; then |
| 1102 | +新增: echo "Error: could not query Lima. Is it installed and working?" >&2 |
| 1103 | +新增: return 1 |
| 1104 | +新增: fi |
| 1105 | +新增: while IFS= read -r line; do |
| 1106 | +新增: [[ -n "$line" ]] || continue |
| 1107 | +新增: if [[ ${#lines[@]} -eq 0 || "$line" == agent-vm-* ]]; then |
| 1108 | +新增: lines+=("$line") |
| 1109 | +新增: [[ ${#line} -gt $w ]] && w=${#line} |
| 1866 | 1110 | fi |
| 1867 | | −删除: done || echo " (no VMs)" |
| 1868 | | −删除:} |
| 1869 | | −删除: |
| 1870 | | −删除:# When the file is executed directly (`./agent-vm.sh setup`) rather than |
| 1871 | | −删除:# sourced, dispatch to the agent-vm function so it doesn't silently no-op. |
| 1872 | | −删除:# Sourcing remains the canonical install path because it makes `agent-vm` |
| 1873 | | −删除:# available as a shell function across all future commands. Detection is |
| 1874 | | −删除:# shell-specific: |
| 1875 | | −删除:# bash: BASH_SOURCE[0] differs from $0 when sourced |
| 1876 | | −删除:# zsh: ZSH_EVAL_CONTEXT contains ':file' when sourced |
| 1877 | | −删除:_agent_vm_is_sourced() { |
| 1878 | | −删除: if [[ -n "${BASH_VERSION:-}" ]]; then |
| 1879 | | −删除: [[ "${BASH_SOURCE[0]}" != "$0" ]] |
| 1880 | | −删除: elif [[ -n "${ZSH_VERSION:-}" ]]; then |
| 1881 | | −删除: [[ "${ZSH_EVAL_CONTEXT:-}" == *:file* ]] |
| 1882 | | −删除: else |
| 1883 | | −删除: return 0 # unknown shell — assume sourced and don't auto-run |
| 1111 | +新增: done <<< "$list" |
| 1112 | +新增: if [[ ${#lines[@]} -lt 2 ]]; then |
| 1113 | +新增: [[ ${#lines[@]} -eq 0 ]] || printf ' %s\n' "${lines[@]}" |
| 1114 | +新增: echo " (no VMs)" |
| 1115 | +新增: return 0 |
| 1884 | 1116 | fi |
| 1117 | +新增: for line in "${lines[@]}"; do |
| 1118 | +新增: if [[ -n "$header" ]]; then |
| 1119 | +新增: header="" |
| 1120 | +新增: printf ' %-*s %s\n' "$w" "$line" "BASE" |
| 1121 | +新增: else |
| 1122 | +新增: mark=" " |
| 1123 | +新增: [[ "${line%% *}" == "$current" ]] && mark=">" |
| 1124 | +新增: printf '%s %-*s %s\n' "$mark" "$w" "$line" "$(_agent_vm_base_label "${line%% *}")" |
| 1125 | +新增: fi |
| 1126 | +新增: done |
| 1885 | 1127 | } |
| 1886 | 1128 | |
| 1887 | | −删除:if ! _agent_vm_is_sourced; then |
| 1888 | | −删除: unset -f _agent_vm_is_sourced |
| 1129 | +新增:# When the file is executed (the command `install` puts on PATH, or |
| 1130 | +新增:# `./agent-vm.sh setup`) rather than sourced, dispatch to the agent-vm |
| 1131 | +新增:# function so it doesn't silently no-op. BASH_SOURCE[0] differs from $0 when |
| 1132 | +新增:# sourced. |
| 1133 | +新增:if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then |
| 1889 | 1134 | agent-vm "$@" |
| 1890 | | −删除: # Preserve agent-vm's exit code — without the explicit exit, the script |
| 1891 | | −删除: # would end on the implicit `unset -f` below, which always returns 0 and |
| 1892 | | −删除: # would mask command failures (`./agent-vm.sh bad-cmd; echo $?` → 0). |
| 1893 | 1135 | exit $? |
| 1894 | 1136 | fi |
| 1895 | | −删除:unset -f _agent_vm_is_sourced |