| @@ -13,6 +13,7 @@ |
| 13 | 13 | # agent-vm codex - Run Codex CLI in a persistent VM for cwd |
| 14 | 14 | # agent-vm vibe - Run Mistral Vibe in a persistent VM for cwd |
| 15 | 15 | # agent-vm shell - Open a shell in the persistent VM for cwd |
| 16 | +新增:# (alias: 'sh'; add -c "..." for a one-shot command) |
| 16 | 17 | # agent-vm stop - Stop the VM for cwd |
| 17 | 18 | # agent-vm rm - Stop and delete the VM for cwd |
| 18 | 19 | # agent-vm list - List all agent-vm VMs |
| @@ -24,6 +25,114 @@ AGENT_VM_TEMPLATE="agent-vm-base" |
| 24 | 25 | AGENT_VM_STATE_DIR="${HOME}/.agent-vm" |
| 25 | 26 | AGENT_VM_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" |
| 26 | 27 | |
| 28 | +新增:# Prompt for a value with a default. Reads from /dev/tty so this still works |
| 29 | +新增:# when called inside command substitution. Writes the prompt to stderr and the |
| 30 | +新增:# answer (or the default if the user just pressed Enter) to stdout. |
| 31 | +新增:_agent_vm_ask() { |
| 32 | +新增: local prompt="$1" default="$2" reply="" |
| 33 | +新增: printf ' %s [%s]: ' "$prompt" "$default" >&2 |
| 34 | +新增: IFS= read -r reply </dev/tty 2>/dev/null || reply="" |
| 35 | +新增: printf '%s\n' "${reply:-$default}" |
| 36 | +新增:} |
| 37 | +新增: |
| 38 | +新增:# Yes/no prompt. Second arg is the default: Y or N (case-insensitive). Prints |
| 39 | +新增:# 1 (yes) or 0 (no) to stdout. Empty input picks the default. |
| 40 | +新增:_agent_vm_ask_yn() { |
| 41 | +新增: local prompt="$1" default="${2:-Y}" reply="" indicator |
| 42 | +新增: case "$default" in |
| 43 | +新增: [Yy]*) indicator="[Y/n]"; default=Y ;; |
| 44 | +新增: *) indicator="[y/N]"; default=N ;; |
| 45 | +新增: esac |
| 46 | +新增: printf ' %s %s: ' "$prompt" "$indicator" >&2 |
| 47 | +新增: IFS= read -r reply </dev/tty 2>/dev/null || reply="" |
| 48 | +新增: reply="${reply:-$default}" |
| 49 | +新增: case "$reply" in |
| 50 | +新增: [Yy]*) printf '1\n' ;; |
| 51 | +新增: *) printf '0\n' ;; |
| 52 | +新增: esac |
| 53 | +新增:} |
| 54 | +新增: |
| 55 | +新增:# Prompt for a positive integer with default. Re-prompts on invalid input. |
| 56 | +新增:# Used for disk/memory/cpus where a typo (e.g. "10G") would otherwise produce |
| 57 | +新增:# a cryptic limactl error several seconds later. |
| 58 | +新增:_agent_vm_ask_int() { |
| 59 | +新增: local prompt="$1" default="$2" reply |
| 60 | +新增: while true; do |
| 61 | +新增: reply=$(_agent_vm_ask "$prompt" "$default") |
| 62 | +新增: if [[ "$reply" =~ ^[1-9][0-9]*$ ]]; then |
| 63 | +新增: printf '%s\n' "$reply" |
| 64 | +新增: return 0 |
| 65 | +新增: fi |
| 66 | +新增: printf ' (must be a positive integer, e.g. 10 — got: %s)\n' "$reply" >&2 |
| 67 | +新增: done |
| 68 | +新增:} |
| 69 | +新增: |
| 70 | +新增:# Validate a positive-integer arg from the CLI (no retry — fail fast). |
| 71 | +新增:_agent_vm_validate_int() { |
| 72 | +新增: local name="$1" val="$2" |
| 73 | +新增: if [[ ! "$val" =~ ^[1-9][0-9]*$ ]]; then |
| 74 | +新增: echo "Error: $name must be a positive integer (got: '$val')" >&2 |
| 75 | +新增: return 1 |
| 76 | +新增: fi |
| 77 | +新增:} |
| 78 | +新增: |
| 79 | +新增:# Check Linux prerequisites Lima needs to spin up a QEMU+KVM VM. macOS uses |
| 80 | +新增:# different backends (vz/qemu-via-brew) so this is a no-op there. Returns |
| 81 | +新增:# non-zero with actionable install/permission hints when something's missing — |
| 82 | +新增:# without this, the user gets a generic `Error: Failed to create base VM` and |
| 83 | +新增:# has to dig into `~/.lima/<vm>/ha.stderr.log` to figure out why. |
| 84 | +新增:_agent_vm_check_linux_prereqs() { |
| 85 | +新增: [[ "$(uname -s)" == "Linux" ]] || return 0 |
| 86 | +新增: |
| 87 | +新增: local arch_bin arch_pkg |
| 88 | +新增: case "$(uname -m)" in |
| 89 | +新增: x86_64) arch_bin="qemu-system-x86_64"; arch_pkg="qemu-system-x86" ;; |
| 90 | +新增: aarch64|arm64) arch_bin="qemu-system-aarch64"; arch_pkg="qemu-system-arm" ;; |
| 91 | +新增: *) arch_bin="qemu-system-$(uname -m)"; arch_pkg="qemu-system" ;; |
| 92 | +新增: esac |
| 93 | +新增: |
| 94 | +新增: local errs=0 |
| 95 | +新增: |
| 96 | +新增: if ! command -v "$arch_bin" &>/dev/null; then |
| 97 | +新增: echo "Error: Lima needs '$arch_bin' on PATH (not found)." >&2 |
| 98 | +新增: echo " Install with: sudo apt-get install $arch_pkg" >&2 |
| 99 | +新增: errs=1 |
| 100 | +新增: fi |
| 101 | +新增: |
| 102 | +新增: if [[ ! -e /dev/kvm ]]; then |
| 103 | +新增: echo "Error: /dev/kvm does not exist (KVM unavailable)." >&2 |
| 104 | +新增: echo " Hardware virtualization may be disabled in BIOS, or the kernel" >&2 |
| 105 | +新增: echo " lacks KVM support (nested virt in a guest VM, etc.)." >&2 |
| 106 | +新增: errs=1 |
| 107 | +新增: elif [[ ! -r /dev/kvm || ! -w /dev/kvm ]]; then |
| 108 | +新增: echo "Error: /dev/kvm exists but you don't have read/write access." >&2 |
| 109 | +新增: if ! id -nG 2>/dev/null | grep -qw kvm; then |
| 110 | +新增: echo " Fix: sudo usermod -aG kvm \"\$USER\"" >&2 |
| 111 | +新增: echo " Then log out and back in (or run 'newgrp kvm') so the new" >&2 |
| 112 | +新增: echo " group membership takes effect." >&2 |
| 113 | +新增: else |
| 114 | +新增: echo " You're already in the kvm group but /dev/kvm denies access." >&2 |
| 115 | +新增: echo " Check ownership/mode: ls -l /dev/kvm" >&2 |
| 116 | +新增: fi |
| 117 | +新增: errs=1 |
| 118 | +新增: fi |
| 119 | +新增: |
| 120 | +新增: [[ $errs -eq 0 ]] |
| 121 | +新增:} |
| 122 | +新增: |
| 123 | +新增:# Lima leaves a partial state dir behind if `limactl create` is interrupted |
| 124 | +新增:# (Ctrl-C before lima.yaml is written). After that every subsequent limactl |
| 125 | +新增:# call on that name dies with `open ~/.lima/<vm>/lima.yaml: no such file or |
| 126 | +新增:# directory` — pre-emptively clean the dir so the next setup/start works. |
| 127 | +新增:_agent_vm_clean_partial_state() { |
| 128 | +新增: local vm_name="$1" |
| 129 | +新增: local lima_dir="$HOME/.lima/$vm_name" |
| 130 | +新增: if [[ -d "$lima_dir" ]] && [[ ! -f "$lima_dir/lima.yaml" ]]; then |
| 131 | +新增: echo "Detected partial VM state at $lima_dir (no lima.yaml) — cleaning up." >&2 |
| 132 | +新增: rm -rf "$lima_dir" |
| 133 | +新增: fi |
| 134 | +新增:} |
| 135 | +新增: |
| 27 | 136 | # Generate a deterministic VM name for a directory |
| 28 | 137 | _agent_vm_name() { |
| 29 | 138 | local dir="${1:-$(pwd)}" |
| @@ -44,6 +153,132 @@ _agent_vm_running() { |
| 44 | 153 | limactl list --format '{{.Name}} {{.Status}}' 2>/dev/null | grep -q "^${1} Running$" |
| 45 | 154 | } |
| 46 | 155 | |
| 156 | +新增:# Stage a single host file at <dst> via hardlink, falling back to copy if the |
| 157 | +新增:# source and destination live on different filesystems. Hardlinking keeps the |
| 158 | +新增:# content live-synced with the host (same inode) without exposing the source's |
| 159 | +新增:# parent directory to the VM. The copy fallback preserves the no-exposure |
| 160 | +新增:# property but loses live sync until the next VM (re)start. |
| 161 | +新增:_agent_vm_stage_file() { |
| 162 | +新增: local src="$1" dst="$2" |
| 163 | +新增: mkdir -p "$(dirname "$dst")" 2>/dev/null || return 1 |
| 164 | +新增: rm -f "$dst" |
| 165 | +新增: if ln "$src" "$dst" 2>/dev/null; then |
| 166 | +新增: return 0 |
| 167 | +新增: fi |
| 168 | +新增: if cp -p "$src" "$dst" 2>/dev/null; then |
| 169 | +新增: echo "Warning: Staged '${src}' via copy (cross-filesystem hardlink failed); live host changes will not propagate until VM (re)start." >&2 |
| 170 | +新增: return 0 |
| 171 | +新增: fi |
| 172 | +新增: return 1 |
| 173 | +新增:} |
| 174 | +新增: |
| 175 | +新增:# Remove all per-VM state files (version marker, terminfo cache, file mount |
| 176 | +新增:# cache, staging dirs). Called after a VM is deleted or before it is re-cloned |
| 177 | +新增:# via --reset. |
| 178 | +新增:_agent_vm_cleanup_state() { |
| 179 | +新增: local vm_name="$1" |
| 180 | +新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 181 | +新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}" |
| 182 | +新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}" |
| 183 | +新增: rm -rf "$AGENT_VM_STATE_DIR/file-mounts/${vm_name}" |
| 184 | +新增:} |
| 185 | +新增: |
| 186 | +新增:# Build the .mounts JSON array for a VM. The first entry is always the project |
| 187 | +新增:# dir (writable). Additional entries come from ~/.agent-vm/volumes, parsed as |
| 188 | +新增:# Docker-Compose-ish `source[:destination][:mode]` (mode ∈ {ro,rw}, default ro). |
| 189 | +新增:# |
| 190 | +新增:# Side effects: stages any file mounts as hardlinks under |
| 191 | +新增:# ~/.agent-vm/file-mounts/<vm>/ and persists the file mount metadata to |
| 192 | +新增:# ~/.agent-vm/.agent-vm-file-mounts-<vm> so subsequent starts can re-apply the |
| 193 | +新增:# inside-VM bind mounts without re-parsing the volumes file. Stdout: the |
| 194 | +新增:# mounts JSON array (consumed by `limactl edit --set ".mounts = ..."`). |
| 195 | +新增:_agent_vm_build_mounts_json() { |
| 196 | +新增: local vm_name="$1" host_dir="$2" |
| 197 | +新增: local mounts_json="[{\"location\": \"${host_dir}\", \"writable\": true}" |
| 198 | +新增: local mounts_file="$AGENT_VM_STATE_DIR/volumes" |
| 199 | +新增: local file_mount_entries=() |
| 200 | +新增: local file_mounts_cache="$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}" |
| 201 | +新增: |
| 202 | +新增: if [[ -f "$mounts_file" ]]; then |
| 203 | +新增: local staging_idx=0 |
| 204 | +新增: while IFS= read -r line || [[ -n "$line" ]]; do |
| 205 | +新增: line="${line%%#*}" # strip comments |
| 206 | +新增: line="${line#"${line%%[![:space:]]*}"}" # trim leading whitespace |
| 207 | +新增: line="${line%"${line##*[![:space:]]}"}" # trim trailing whitespace |
| 208 | +新增: [[ -z "$line" ]] && continue |
| 209 | +新增: # Parse source[:destination][:mode] syntax (like docker compose volumes). |
| 210 | +新增: # The trailing mode segment is only recognized when it equals "ro" or |
| 211 | +新增: # "rw" — anything else is treated as a destination path. |
| 212 | +新增: local src="$line" dst="" mode="ro" |
| 213 | +新增: if [[ "$line" == *:ro || "$line" == *:rw ]]; then |
| 214 | +新增: mode="${line##*:}" |
| 215 | +新增: line="${line%:*}" |
| 216 | +新增: fi |
| 217 | +新增: if [[ "$line" == *:* ]]; then |
| 218 | +新增: src="${line%%:*}" |
| 219 | +新增: dst="${line#*:}" |
| 220 | +新增: else |
| 221 | +新增: src="$line" |
| 222 | +新增: fi |
| 223 | +新增: src="${src/#\~/$HOME}" # expand ~ |
| 224 | +新增: # Reject characters that would break JSON interpolation below or the |
| 225 | +新增: # pipe-separated cache format used for file mounts. |
| 226 | +新增: if [[ "$src" == *[$'"\\\n|']* || "$dst" == *[$'"\\\n|']* ]]; then |
| 227 | +新增: echo "Warning: Mount entry '${line}' (from ~/.agent-vm/volumes) contains invalid characters (quote/backslash/newline/pipe), skipping." >&2 |
| 228 | +新增: continue |
| 229 | +新增: fi |
| 230 | +新增: if [[ ! -e "$src" ]]; then |
| 231 | +新增: echo "Warning: Mount path '${src}' (from ~/.agent-vm/volumes) does not exist, skipping." >&2 |
| 232 | +新增: continue |
| 233 | +新增: fi |
| 234 | +新增: if [[ -f "$src" ]]; then |
| 235 | +新增: if [[ "$mode" == "rw" ]]; then |
| 236 | +新增: 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 |
| 237 | +新增: continue |
| 238 | +新增: fi |
| 239 | +新增: # File mount: hardlink the source into a per-VM host staging dir so |
| 240 | +新增: # the VM sees only this file (never the source's parent). Lima mounts |
| 241 | +新增: # the staging dir via virtiofs; a bind mount inside the VM (applied |
| 242 | +新增: # after boot) exposes the file at its final destination. |
| 243 | +新增: local filename |
| 244 | +新增: filename="$(basename "$src")" |
| 245 | +新增: local file_staging_dir="$AGENT_VM_STATE_DIR/file-mounts/${vm_name}/${staging_idx}" |
| 246 | +新增: local host_staging="${file_staging_dir}/${filename}" |
| 247 | +新增: if ! _agent_vm_stage_file "$src" "$host_staging"; then |
| 248 | +新增: echo "Warning: Failed to stage '${src}', skipping." >&2 |
| 249 | +新增: rm -rf "$file_staging_dir" |
| 250 | +新增: continue |
| 251 | +新增: fi |
| 252 | +新增: local staging_mount="/tmp/.agent-vm-file-mounts/${staging_idx}" |
| 253 | +新增: local bind_dst="${dst:-${src}}" |
| 254 | +新增: file_mount_entries+=("${src}|${host_staging}|${staging_mount}/${filename}|${bind_dst}") |
| 255 | +新增: mounts_json+=", {\"location\": \"${file_staging_dir}\", \"mountPoint\": \"${staging_mount}\", \"writable\": false}" |
| 256 | +新增: staging_idx=$((staging_idx + 1)) |
| 257 | +新增: continue |
| 258 | +新增: fi |
| 259 | +新增: if [[ ! -d "$src" ]]; then |
| 260 | +新增: echo "Warning: Mount path '${src}' (from ~/.agent-vm/volumes) is not a regular file or directory, skipping." >&2 |
| 261 | +新增: continue |
| 262 | +新增: fi |
| 263 | +新增: local writable="false" |
| 264 | +新增: [[ "$mode" == "rw" ]] && writable="true" |
| 265 | +新增: if [[ -n "$dst" ]]; then |
| 266 | +新增: mounts_json+=", {\"location\": \"${src}\", \"mountPoint\": \"${dst}\", \"writable\": ${writable}}" |
| 267 | +新增: else |
| 268 | +新增: mounts_json+=", {\"location\": \"${src}\", \"writable\": ${writable}}" |
| 269 | +新增: fi |
| 270 | +新增: done < "$mounts_file" |
| 271 | +新增: fi |
| 272 | +新增: mounts_json+="]" |
| 273 | +新增: |
| 274 | +新增: rm -f "$file_mounts_cache" |
| 275 | +新增: if [[ ${#file_mount_entries[@]} -gt 0 ]]; then |
| 276 | +新增: printf '%s\n' "${file_mount_entries[@]}" > "$file_mounts_cache" |
| 277 | +新增: fi |
| 278 | +新增: |
| 279 | +新增: printf '%s' "$mounts_json" |
| 280 | +新增:} |
| 281 | +新增: |
| 47 | 282 | # Print VM resource details (CPUs, memory, disk) |
| 48 | 283 | _agent_vm_print_resources() { |
| 49 | 284 | local vm_name="$1" |
| @@ -89,6 +324,11 @@ _agent_vm_ensure_running() { |
| 89 | 324 | return 1 |
| 90 | 325 | fi |
| 91 | 326 | |
| 327 | +新增: # Recover from an interrupted previous run that left ~/.lima/<vm>/ without |
| 328 | +新增: # a lima.yaml — otherwise every limactl call on this name aborts with a |
| 329 | +新增: # cryptic "no such file or directory". |
| 330 | +新增: _agent_vm_clean_partial_state "$vm_name" |
| 331 | +新增: |
| 92 | 332 | if ! limactl list -q 2>/dev/null | grep -q "^${AGENT_VM_TEMPLATE}$"; then |
| 93 | 333 | echo "Error: Base VM not found. Run 'agent-vm setup' first." >&2 |
| 94 | 334 | return 1 |
| @@ -99,18 +339,24 @@ _agent_vm_ensure_running() { |
| 99 | 339 | echo "Resetting VM '$vm_name'..." |
| 100 | 340 | limactl stop "$vm_name" &>/dev/null |
| 101 | 341 | limactl delete "$vm_name" --force &>/dev/null |
| 102 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 103 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}" |
| 342 | +新增: _agent_vm_cleanup_state "$vm_name" |
| 104 | 343 | fi |
| 105 | 344 | |
| 345 | +新增: local is_new_vm="" |
| 346 | +新增: local file_mount_entries=() |
| 347 | +新增: local file_mounts_cache="$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}" |
| 348 | +新增: |
| 106 | 349 | if ! _agent_vm_exists "$vm_name"; then |
| 350 | +新增: is_new_vm=1 |
| 107 | 351 | echo "Creating VM '$vm_name'..." |
| 108 | 352 | limactl clone "$AGENT_VM_TEMPLATE" "$vm_name" --tty=false &>/dev/null |
| 109 | 353 | # Apply mount and resource settings via edit after clone |
| 110 | 354 | # Mount and memory/cpus are applied separately from disk, because |
| 111 | 355 | # Lima rejects the entire edit if disk shrinking is attempted. |
| 356 | +新增: local mounts_json |
| 357 | +新增: mounts_json=$(_agent_vm_build_mounts_json "$vm_name" "$host_dir") |
| 112 | 358 | local edit_args=() |
| 113 | | −删除: edit_args+=(--set ".mounts = [{\"location\": \"${host_dir}\", \"writable\": true}]") |
| 359 | +新增: edit_args+=(--set ".mounts = ${mounts_json}") |
| 114 | 360 | [[ -n "$memory" ]] && edit_args+=(--memory "$memory") |
| 115 | 361 | [[ -n "$cpus" ]] && edit_args+=(--cpus "$cpus") |
| 116 | 362 | (cd /tmp && limactl edit "$vm_name" "${edit_args[@]}") &>/dev/null |
| @@ -129,9 +375,9 @@ _agent_vm_ensure_running() { |
| 129 | 375 | # Auto-resize existing VM if --disk, --memory, or --cpus changed |
| 130 | 376 | if _agent_vm_running "$vm_name"; then |
| 131 | 377 | echo "VM '$vm_name' is currently running. It must be stopped to apply new resource settings." |
| 132 | | −删除: printf "Stop the VM and apply changes? [y/N] " |
| 133 | | −删除: local reply |
| 134 | | −删除: read -r reply |
| 378 | +新增: printf "Stop the VM and apply changes? [y/N] " >&2 |
| 379 | +新增: local reply="" |
| 380 | +新增: IFS= read -r reply </dev/tty 2>/dev/null || reply="" |
| 135 | 381 | if [[ ! "$reply" =~ ^[Yy]$ ]]; then |
| 136 | 382 | echo "Aborted. Starting with current settings." |
| 137 | 383 | return 0 |
| @@ -140,8 +386,9 @@ _agent_vm_ensure_running() { |
| 140 | 386 | limactl stop "$vm_name" &>/dev/null |
| 141 | 387 | fi |
| 142 | 388 | echo "Updating VM resources..." |
| 389 | +新增: # Don't touch .mounts here — those are baked in at creation (including any |
| 390 | +新增: # entries from ~/.agent-vm/volumes). Re-setting them would clobber extras. |
| 143 | 391 | local edit_args=() |
| 144 | | −删除: edit_args+=(--set ".mounts = [{\"location\": \"${host_dir}\", \"writable\": true}]") |
| 145 | 392 | [[ -n "$memory" ]] && edit_args+=(--memory "$memory") |
| 146 | 393 | [[ -n "$cpus" ]] && edit_args+=(--cpus "$cpus") |
| 147 | 394 | local edit_output |
| @@ -167,7 +414,14 @@ _agent_vm_ensure_running() { |
| 167 | 414 | |
| 168 | 415 | if ! _agent_vm_running "$vm_name"; then |
| 169 | 416 | echo "Starting VM '$vm_name'..." |
| 170 | | −删除: limactl start "$vm_name" &>/dev/null |
| 417 | +新增: local start_log |
| 418 | +新增: if ! start_log=$(limactl start "$vm_name" 2>&1); then |
| 419 | +新增: echo "Error: Failed to start VM '$vm_name'." >&2 |
| 420 | +新增: echo "--- limactl start output ---" >&2 |
| 421 | +新增: echo "$start_log" >&2 |
| 422 | +新增: echo "Full log: ~/.lima/$vm_name/ha.stderr.log" >&2 |
| 423 | +新增: return 1 |
| 424 | +新增: fi |
| 171 | 425 | fi |
| 172 | 426 | |
| 173 | 427 | # Sanity-check that the project directory is actually shared and writable. |
| @@ -184,8 +438,13 @@ _agent_vm_ensure_running() { |
| 184 | 438 | if ! limactl shell "$vm_name" test -w "$host_dir" &>/dev/null; then |
| 185 | 439 | echo "Project mount is not writable; repairing..." >&2 |
| 186 | 440 | limactl stop "$vm_name" &>/dev/null |
| 441 | +新增: # Rebuild the full mounts JSON so any ~/.agent-vm/volumes entries are |
| 442 | +新增: # preserved across the repair (a plain project-dir-only set would silently |
| 443 | +新增: # drop them). |
| 444 | +新增: local repair_mounts_json |
| 445 | +新增: repair_mounts_json=$(_agent_vm_build_mounts_json "$vm_name" "$host_dir") |
| 187 | 446 | (cd /tmp && limactl edit "$vm_name" \ |
| 188 | | −删除: --set ".mounts = [{\"location\": \"${host_dir}\", \"writable\": true}]") &>/dev/null |
| 447 | +新增: --set ".mounts = ${repair_mounts_json}") &>/dev/null |
| 189 | 448 | limactl start "$vm_name" &>/dev/null |
| 190 | 449 | if ! limactl shell "$vm_name" test -w "$host_dir" &>/dev/null; then |
| 191 | 450 | echo "Error: project directory is still not writable inside the VM:" >&2 |
| @@ -210,6 +469,18 @@ _agent_vm_ensure_running() { |
| 210 | 469 | fi |
| 211 | 470 | fi |
| 212 | 471 | |
| 472 | +新增: # Push ~/.agent-vm/env (a dotenv-style file of tokens / API keys) into the VM |
| 473 | +新增: # at $HOME/.agent-vm.env on every start, so updates on the host propagate |
| 474 | +新增: # without --reset. The base VM's ~/.zshenv auto-sources it via `set -a`, so |
| 475 | +新增: # the contents stay a plain KEY=value file (no `export` needed). `umask 077` |
| 476 | +新增: # creates the file mode-600 since it usually holds secrets. |
| 477 | +新增: if [ -f "$AGENT_VM_STATE_DIR/env" ]; then |
| 478 | +新增: if ! limactl shell "$vm_name" sh -c 'umask 077 && rm -f "$HOME/.agent-vm.env" && cat > "$HOME/.agent-vm.env"' \ |
| 479 | +新增: < "$AGENT_VM_STATE_DIR/env" 2>/dev/null; then |
| 480 | +新增: echo "Warning: failed to push ~/.agent-vm/env into VM '$vm_name'." >&2 |
| 481 | +新增: fi |
| 482 | +新增: fi |
| 483 | +新增: |
| 213 | 484 | # Run per-user runtime script if it exists |
| 214 | 485 | if [ -f "$AGENT_VM_STATE_DIR/runtime.sh" ]; then |
| 215 | 486 | echo "Running user runtime setup..." |
| @@ -234,14 +505,84 @@ _agent_vm_ensure_running() { |
| 234 | 505 | fi |
| 235 | 506 | |
| 236 | 507 | if [[ -n "$rdonly" ]]; then |
| 508 | +新增: # `mount -o remount,ro` on the host share fails silently when Lima uses |
| 509 | +新增: # a FUSE-backed mount (sshfs/virtiofs in some configurations) — FUSE |
| 510 | +新增: # rejects the `remount` option and the FS stays writable. Self-bind the |
| 511 | +新增: # directory first so we have a plain Linux bind-mount layer to remount, |
| 512 | +新增: # which works regardless of what's underneath. |
| 237 | 513 | echo "Mounting project directory as read-only..." |
| 238 | | −删除: limactl shell "$vm_name" sudo mount -o remount,ro "$host_dir" |
| 514 | +新增: if ! limactl shell "$vm_name" sudo mount --bind "$host_dir" "$host_dir" \ |
| 515 | +新增: || ! limactl shell "$vm_name" sudo mount -o remount,ro,bind "$host_dir"; then |
| 516 | +新增: echo "Error: Failed to mount project directory as read-only." >&2 |
| 517 | +新增: return 1 |
| 518 | +新增: fi |
| 239 | 519 | fi |
| 240 | 520 | |
| 241 | 521 | if [[ -n "$git_ro" ]] && [[ -d "$host_dir/.git" ]]; then |
| 242 | 522 | echo "Mounting .git directory as read-only..." |
| 243 | | −删除: limactl shell "$vm_name" sudo mount --bind "$host_dir/.git" "$host_dir/.git" |
| 244 | | −删除: limactl shell "$vm_name" sudo mount -o remount,ro,bind "$host_dir/.git" |
| 523 | +新增: if ! limactl shell "$vm_name" sudo mount --bind "$host_dir/.git" "$host_dir/.git" \ |
| 524 | +新增: || ! limactl shell "$vm_name" sudo mount -o remount,ro,bind "$host_dir/.git"; then |
| 525 | +新增: echo "Error: Failed to mount .git directory as read-only." >&2 |
| 526 | +新增: return 1 |
| 527 | +新增: fi |
| 528 | +新增: fi |
| 529 | +新增: |
| 530 | +新增: # Load file mount entries from the cache. _agent_vm_build_mounts_json writes |
| 531 | +新增: # them there (from its own local scope) for both new and existing VMs, so we |
| 532 | +新增: # always read them back here to drive the inside-VM bind mounts below. |
| 533 | +新增: if [[ -f "$file_mounts_cache" ]]; then |
| 534 | +新增: local entry |
| 535 | +新增: while IFS= read -r entry; do |
| 536 | +新增: [[ -n "$entry" ]] && file_mount_entries+=("$entry") |
| 537 | +新增: done < "$file_mounts_cache" |
| 538 | +新增: |
| 539 | +新增: # For existing VMs, refresh host-side hardlinks so atomic-rename edits on the |
| 540 | +新增: # host propagate after a VM restart (ln/cp against the cached staging path). |
| 541 | +新增: # New VMs just staged fresh copies in _agent_vm_build_mounts_json, so there |
| 542 | +新增: # is nothing to refresh. |
| 543 | +新增: if [[ -z "$is_new_vm" ]]; then |
| 544 | +新增: local host_src host_staging _bind_src _bind_dst |
| 545 | +新增: for entry in "${file_mount_entries[@]}"; do |
| 546 | +新增: IFS='|' read -r host_src host_staging _bind_src _bind_dst <<< "$entry" |
| 547 | +新增: [[ -z "$host_staging" ]] && continue |
| 548 | +新增: if [[ ! -e "$host_src" ]]; then |
| 549 | +新增: echo "Warning: Mount source '${host_src}' no longer exists; VM will see the last-staged copy." >&2 |
| 550 | +新增: continue |
| 551 | +新增: fi |
| 552 | +新增: _agent_vm_stage_file "$host_src" "$host_staging" \ |
| 553 | +新增: || echo "Warning: Failed to refresh staged '${host_src}'; VM may see stale content." >&2 |
| 554 | +新增: done |
| 555 | +新增: fi |
| 556 | +新增: fi |
| 557 | +新增: |
| 558 | +新增: # Apply inside-VM bind mounts so each staged file appears at its final path. |
| 559 | +新增: # Lima re-mounts staging dirs on each start, but the bind onto the final dest |
| 560 | +新增: # is ephemeral. Batched into one limactl shell call (roundtrips cost ~1-2s) |
| 561 | +新增: # and made idempotent so re-runs on a running VM are cheap no-ops. |
| 562 | +新增: if [[ ${#file_mount_entries[@]} -gt 0 ]]; then |
| 563 | +新增: local file_bind_payload=() |
| 564 | +新增: local _host_src _host_staging bind_src bind_dst |
| 565 | +新增: for entry in "${file_mount_entries[@]}"; do |
| 566 | +新增: IFS='|' read -r _host_src _host_staging bind_src bind_dst <<< "$entry" |
| 567 | +新增: [[ -n "$bind_src" && -n "$bind_dst" ]] && file_bind_payload+=("${bind_src}|${bind_dst}") |
| 568 | +新增: done |
| 569 | +新增: if [[ ${#file_bind_payload[@]} -gt 0 ]]; then |
| 570 | +新增: echo "Mounting individual files..." |
| 571 | +新增: # Paths are passed as positional args (single-quoted script) so entries |
| 572 | +新增: # containing quotes or metacharacters cannot be interpreted as shell code. |
| 573 | +新增: limactl shell "$vm_name" sudo bash -c ' |
| 574 | +新增: set -e |
| 575 | +新增: for entry in "$@"; do |
| 576 | +新增: bind_src="${entry%%|*}" |
| 577 | +新增: bind_dst="${entry#*|}" |
| 578 | +新增: if ! findmnt -no TARGET "$bind_dst" >/dev/null 2>&1; then |
| 579 | +新增: mkdir -p "$(dirname "$bind_dst")" && touch "$bind_dst" |
| 580 | +新增: mount --bind "$bind_src" "$bind_dst" |
| 581 | +新增: mount -o remount,ro,bind "$bind_dst" |
| 582 | +新增: fi |
| 583 | +新增: done |
| 584 | +新增: ' -- "${file_bind_payload[@]}" |
| 585 | +新增: fi |
| 245 | 586 | fi |
| 246 | 587 | } |
| 247 | 588 | |
| @@ -309,7 +650,7 @@ agent-vm() { |
| 309 | 650 | vibe) |
| 310 | 651 | _agent_vm_vibe "${vm_opts[@]}" "$@" |
| 311 | 652 | ;; |
| 312 | | −删除: shell) |
| 653 | +新增: shell|sh) |
| 313 | 654 | _agent_vm_shell "${vm_opts[@]}" "$@" |
| 314 | 655 | ;; |
| 315 | 656 | run) |
| @@ -351,8 +692,11 @@ Commands: |
| 351 | 692 | opencode [args] Run OpenCode in the VM for the current directory |
| 352 | 693 | codex [args] Run Codex CLI in the VM for the current directory |
| 353 | 694 | vibe [args] Run Mistral Vibe in the VM for the current directory |
| 354 | | −删除: shell Open a shell in the VM for the current directory |
| 355 | | −删除: run <cmd> [args] Run a command in the VM for the current directory |
| 695 | +新增: shell, sh Open a shell in the VM. Add -c "..." to run a one-shot |
| 696 | +新增: command via login zsh and exit. |
| 697 | +新增: run <cmd> [args] Run a command in the VM (no shell — for pipes/redirects |
| 698 | +新增: use 'shell -c "..."' instead; pass --tty for TUIs like |
| 699 | +新增: opencode, vibe, htop, etc.) |
| 356 | 700 | stop Stop the VM for the current directory |
| 357 | 701 | rm Stop and delete the VM for the current directory |
| 358 | 702 | destroy-all Stop and delete all agent-vm VMs |
| @@ -383,13 +727,19 @@ Examples: |
| 383 | 727 | agent-vm --readonly shell # Read-only project mount |
| 384 | 728 | agent-vm --git-ro claude # Protect .git from writes |
| 385 | 729 | agent-vm shell # Shell into the VM |
| 730 | +新增: agent-vm sh -c "ls -la | grep config" # One-shot command via login zsh |
| 386 | 731 | agent-vm run npm install # Run a command in the VM |
| 732 | +新增: agent-vm run --tty opencode -p "..." # Run a TUI with PTY allocated |
| 387 | 733 | agent-vm claude -p "fix lint errors" # Pass args to claude |
| 388 | 734 | |
| 389 | 735 | VMs are persistent and unique per directory. Running "agent-vm shell" or |
| 390 | 736 | "agent-vm claude" in the same directory will reuse the same VM. |
| 391 | 737 | |
| 392 | 738 | Customization: |
| 739 | +新增: ~/.agent-vm/env Shared env vars / tokens (dotenv-style; |
| 740 | +新增: auto-loaded into every VM shell) |
| 741 | +新增: ~/.agent-vm/volumes Extra host paths to mount in VMs (one per |
| 742 | +新增: line, supports both directories and files) |
| 393 | 743 | ~/.agent-vm/setup.sh Per-user setup (runs during "agent-vm setup") |
| 394 | 744 | ~/.agent-vm/runtime.sh Per-user runtime (runs on each VM start) |
| 395 | 745 | <project>/.agent-vm.runtime.sh Per-project runtime (runs on each VM start) |
| @@ -402,55 +752,169 @@ _agent_vm_setup() { |
| 402 | 752 | local disk=10 |
| 403 | 753 | local memory=3 |
| 404 | 754 | local cpus=1 |
| 755 | +新增: local preinstall="" |
| 756 | +新增: local preinstall_seen="" |
| 757 | +新增: # Defaults match the "default install" set: every component on EXCEPT the |
| 758 | +新增: # opt-in languages (Ruby, Rust, Go). These apply when --preinstall isn't |
| 759 | +新增: # passed and either the wizard's first prompt is accepted or stdin is not a |
| 760 | +新增: # terminal (e.g. CI). `--preinstall=all` turns everything on; |
| 761 | +新增: # `--preinstall=default,rust` composes the default set with an opt-in. |
| 762 | +新增: local install_python=1 install_node=1 |
| 763 | +新增: local install_ruby=0 install_rust=0 install_golang=0 |
| 764 | +新增: local install_docker=1 install_chromium=1 install_gh=1 |
| 765 | +新增: local install_claude=1 install_opencode=1 install_codex=1 install_vibe=1 |
| 405 | 766 | |
| 406 | 767 | while [[ $# -gt 0 ]]; do |
| 407 | 768 | case "$1" in |
| 408 | 769 | --help|-h) |
| 409 | | −删除: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N]" |
| 410 | | −删除: echo "" |
| 411 | | −删除: echo "Create a base VM template with dev tools and agents pre-installed." |
| 412 | | −删除: echo "" |
| 413 | | −删除: echo "Options:" |
| 414 | | −删除: echo " --disk GB VM disk size (default: 10)" |
| 415 | | −删除: echo " --memory GB VM memory (default: 3)" |
| 416 | | −删除: echo " --cpus N Number of CPUs (default: 1)" |
| 417 | | −删除: echo " --help Show this help" |
| 770 | +新增: cat << 'EOF' |
| 771 | +新增:Usage: agent-vm setup [options] |
| 772 | +新增: |
| 773 | +新增:Create a base VM template with dev tools and agents pre-installed. Runs an |
| 774 | +新增:interactive wizard by default; the first prompt offers a "default install" |
| 775 | +新增:(everything except the opt-in languages Ruby, Rust, Go). Answer 'n' for |
| 776 | +新增:per-component prompts. Pass --preinstall=... to skip the wizard and pick a |
| 777 | +新增:specific subset non-interactively. When stdin is not a terminal (e.g. CI), |
| 778 | +新增:the wizard is skipped automatically and the default set is installed. |
| 779 | +新增: |
| 780 | +新增:Options: |
| 781 | +新增: --disk GB VM disk size (default: 10) |
| 782 | +新增: --memory GB VM memory (default: 3) |
| 783 | +新增: --cpus N Number of CPUs (default: 1) |
| 784 | +新增: --preinstall=LIST Comma-separated list of tools to preinstall in the base |
| 785 | +新增: VM image (skips the wizard). Anything not listed is |
| 786 | +新增: skipped. Use: |
| 787 | +新增: 'default' for the default set |
| 788 | +新增: (everything except Ruby, Rust, Go), |
| 789 | +新增: 'all' for everything, |
| 790 | +新增: 'none' for nothing. |
| 791 | +新增: Available names: |
| 792 | +新增: python, node, ruby, rust, golang, docker, chromium, |
| 793 | +新增: gh, claude, opencode, codex, vibe |
| 794 | +新增: Selecting codex, or chromium with any AI agent, also |
| 795 | +新增: installs node because those paths require npm/npx. |
| 796 | +新增: Examples: |
| 797 | +新增: --preinstall=default,rust # default set plus Rust |
| 798 | +新增: --preinstall=python,docker,claude |
| 799 | +新增: --help Show this help |
| 800 | +新增:EOF |
| 418 | 801 | return 0 |
| 419 | 802 | ;; |
| 420 | 803 | --disk) |
| 421 | 804 | disk="$2" |
| 422 | 805 | shift 2 |
| 806 | +新增: _agent_vm_validate_int --disk "$disk" || return 1 |
| 423 | 807 | ;; |
| 424 | 808 | --disk=*) |
| 425 | 809 | disk="${1#*=}" |
| 426 | 810 | shift |
| 811 | +新增: _agent_vm_validate_int --disk "$disk" || return 1 |
| 427 | 812 | ;; |
| 428 | 813 | --memory|--ram) |
| 429 | 814 | memory="$2" |
| 430 | 815 | shift 2 |
| 816 | +新增: _agent_vm_validate_int --memory "$memory" || return 1 |
| 431 | 817 | ;; |
| 432 | 818 | --memory=*|--ram=*) |
| 433 | 819 | memory="${1#*=}" |
| 434 | 820 | shift |
| 821 | +新增: _agent_vm_validate_int --memory "$memory" || return 1 |
| 435 | 822 | ;; |
| 436 | 823 | --cpus) |
| 437 | 824 | cpus="$2" |
| 438 | 825 | shift 2 |
| 826 | +新增: _agent_vm_validate_int --cpus "$cpus" || return 1 |
| 439 | 827 | ;; |
| 440 | 828 | --cpus=*) |
| 441 | 829 | cpus="${1#*=}" |
| 442 | 830 | shift |
| 831 | +新增: _agent_vm_validate_int --cpus "$cpus" || return 1 |
| 832 | +新增: ;; |
| 833 | +新增: --preinstall) |
| 834 | +新增: preinstall_seen=1 |
| 835 | +新增: # Only consume the next argument as the value when it is an actual |
| 836 | +新增: # value, not another option. Bare `--preinstall` falls back to the |
| 837 | +新增: # default set (handled below) without swallowing e.g. a following |
| 838 | +新增: # `--disk`. |
| 839 | +新增: if [[ -n "$2" && "$2" != -* ]]; then |
| 840 | +新增: preinstall="$2" |
| 841 | +新增: shift 2 |
| 842 | +新增: else |
| 843 | +新增: shift |
| 844 | +新增: fi |
| 845 | +新增: ;; |
| 846 | +新增: --preinstall=*) |
| 847 | +新增: preinstall="${1#*=}" |
| 848 | +新增: preinstall_seen=1 |
| 849 | +新增: shift |
| 443 | 850 | ;; |
| 444 | 851 | --reset|--offline|--readonly|--git-read-only|--git-ro) |
| 445 | 852 | shift ;; |
| 446 | 853 | *) |
| 447 | 854 | echo "Unknown option: $1" >&2 |
| 448 | | −删除: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N]" >&2 |
| 855 | +新增: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N] [--preinstall=LIST]" >&2 |
| 449 | 856 | return 1 |
| 450 | 857 | ;; |
| 451 | 858 | esac |
| 452 | 859 | done |
| 453 | 860 | |
| 861 | +新增: # Apply --preinstall=LIST: start with everything OFF and turn on what's |
| 862 | +新增: # listed. 'default' / 'all' / 'none' are shortcuts. Setting this also |
| 863 | +新增: # bypasses the interactive wizard below. `--preinstall=` with no value |
| 864 | +新增: # (or `--preinstall` swallowed by a following flag) falls back to the |
| 865 | +新增: # default set — explicitly opting into the wizard's recommended install. |
| 866 | +新增: # Pass `--preinstall=none` if you really want nothing. |
| 867 | +新增: if [[ -n "$preinstall_seen" ]]; then |
| 868 | +新增: install_python=0 install_node=0 install_ruby=0 install_rust=0 install_golang=0 |
| 869 | +新增: install_docker=0 install_chromium=0 install_gh=0 |
| 870 | +新增: install_claude=0 install_opencode=0 install_codex=0 install_vibe=0 |
| 871 | +新增: [[ -z "$preinstall" ]] && preinstall="default" |
| 872 | +新增: # Iterate the comma-list portably across bash and zsh by appending a |
| 873 | +新增: # trailing comma and peeling off one token per iteration. |
| 874 | +新增: local rest="${preinstall}," f |
| 875 | +新增: while [[ -n "$rest" ]]; do |
| 876 | +新增: f="${rest%%,*}" |
| 877 | +新增: rest="${rest#*,}" |
| 878 | +新增: # Trim whitespace. Use bash/zsh-portable substitutions only. |
| 879 | +新增: f="${f# }"; f="${f% }" |
| 880 | +新增: [[ -z "$f" ]] && continue |
| 881 | +新增: case "$f" in |
| 882 | +新增: all) |
| 883 | +新增: install_python=1 install_node=1 install_ruby=1 |
| 884 | +新增: install_rust=1 install_golang=1 |
| 885 | +新增: install_docker=1 install_chromium=1 install_gh=1 |
| 886 | +新增: install_claude=1 install_opencode=1 install_codex=1 install_vibe=1 |
| 887 | +新增: ;; |
| 888 | +新增: default) |
| 889 | +新增: install_python=1 install_node=1 |
| 890 | +新增: install_docker=1 install_chromium=1 install_gh=1 |
| 891 | +新增: install_claude=1 install_opencode=1 install_codex=1 install_vibe=1 |
| 892 | +新增: ;; |
| 893 | +新增: none) ;; # explicit no-op token; with the all-off reset above, |
| 894 | +新增: # `--preinstall=none` ships nothing. |
| 895 | +新增: python) install_python=1 ;; |
| 896 | +新增: node) install_node=1 ;; |
| 897 | +新增: ruby) install_ruby=1 ;; |
| 898 | +新增: rust) install_rust=1 ;; |
| 899 | +新增: golang) install_golang=1 ;; |
| 900 | +新增: docker) install_docker=1 ;; |
| 901 | +新增: chromium) install_chromium=1 ;; |
| 902 | +新增: gh) install_gh=1 ;; |
| 903 | +新增: claude) install_claude=1 ;; |
| 904 | +新增: opencode) install_opencode=1 ;; |
| 905 | +新增: codex) install_codex=1 ;; |
| 906 | +新增: vibe) install_vibe=1 ;; |
| 907 | +新增: *) |
| 908 | +新增: echo "Unknown preinstall name: $f (names are lowercase)" >&2 |
| 909 | +新增: echo "Valid: python, node, ruby, rust, golang, docker, chromium, gh, claude, opencode, codex, vibe, default, all, none" >&2 |
| 910 | +新增: return 1 |
| 911 | +新增: ;; |
| 912 | +新增: esac |
| 913 | +新增: done |
| 914 | +新增: fi |
| 915 | +新增: |
| 916 | +新增: # Fail-fast checks before the (potentially long) wizard so the user doesn't |
| 917 | +新增: # answer 15 prompts only to be told their host is missing Lima or KVM. |
| 454 | 918 | if ! command -v limactl &>/dev/null; then |
| 455 | 919 | if command -v brew &>/dev/null; then |
| 456 | 920 | echo "Installing Lima..." |
| @@ -461,6 +925,102 @@ _agent_vm_setup() { |
| 461 | 925 | fi |
| 462 | 926 | fi |
| 463 | 927 | |
| 928 | +新增: _agent_vm_check_linux_prereqs || return 1 |
| 929 | +新增: |
| 930 | +新增: # Interactive wizard, unless --preinstall was passed or no terminal is |
| 931 | +新增: # attached (e.g. running under CI). Defaults shown in [] are prefilled from |
| 932 | +新增: # any --disk/--memory/--cpus flags the user already passed, so they can |
| 933 | +新增: # confirm or override. Components default to the "default install" set |
| 934 | +新增: # (everything except Ruby/Rust/Go). The first prompt offers that whole set |
| 935 | +新增: # as a one-tap shortcut — answer 'n' for per-component prompts. |
| 936 | +新增: if [[ -z "$preinstall_seen" ]] && [[ -r /dev/tty ]]; then |
| 937 | +新增: printf '\nagent-vm setup wizard\n' >&2 |
| 938 | +新增: printf '─────────────────────\n\n' >&2 |
| 939 | +新增: printf 'These settings apply to the base VM image. Every per-project VM is\n' >&2 |
| 940 | +新增: printf 'cloned from it, so anything preinstalled here is available in all\n' >&2 |
| 941 | +新增: printf 'future agent VMs. You can still install extra tools inside any\n' >&2 |
| 942 | +新增: printf 'individual VM later (e.g. via `agent-vm shell`).\n\n' >&2 |
| 943 | +新增: printf 'For more: https://github.com/sylvinus/agent-vm\n\n' >&2 |
| 944 | +新增: |
| 945 | +新增: # Software first — the more interesting choice for most users. |
| 946 | +新增: printf 'Software\n' >&2 |
| 947 | +新增: printf '────────\n' >&2 |
| 948 | +新增: printf ' Install: Python, Node.js, Docker, Chromium, gh,\n' >&2 |
| 949 | +新增: printf ' Claude Code, OpenCode, Codex CLI, Mistral Vibe\n' >&2 |
| 950 | +新增: printf ' Skip: Ruby, Rust, Go\n\n' >&2 |
| 951 | +新增: local use_default_software |
| 952 | +新增: use_default_software=$(_agent_vm_ask_yn "Use this default" Y) |
| 953 | +新增: if [[ "$use_default_software" != "1" ]]; then |
| 954 | +新增: printf '\nAI coding agents\n' >&2 |
| 955 | +新增: printf '────────────────\n' >&2 |
| 956 | +新增: install_claude=$(_agent_vm_ask_yn "Claude Code" Y) |
| 957 | +新增: install_opencode=$(_agent_vm_ask_yn "OpenCode" Y) |
| 958 | +新增: install_codex=$(_agent_vm_ask_yn "Codex CLI" Y) |
| 959 | +新增: install_vibe=$(_agent_vm_ask_yn "Mistral Vibe" Y) |
| 960 | +新增: |
| 961 | +新增: printf '\nSystem tools\n' >&2 |
| 962 | +新增: printf '────────────\n' >&2 |
| 963 | +新增: install_docker=$(_agent_vm_ask_yn "Docker" Y) |
| 964 | +新增: install_chromium=$(_agent_vm_ask_yn "Chromium (headless browser)" Y) |
| 965 | +新增: install_gh=$(_agent_vm_ask_yn "GitHub CLI (gh)" Y) |
| 966 | +新增: |
| 967 | +新增: local node_forced_reason="" |
| 968 | +新增: if [[ "$install_codex" == "1" ]]; then |
| 969 | +新增: node_forced_reason="Codex CLI requires Node.js" |
| 970 | +新增: elif [[ "$install_chromium" == "1" && ( "$install_claude" == "1" || "$install_opencode" == "1" || "$install_vibe" == "1" ) ]]; then |
| 971 | +新增: node_forced_reason="Chrome DevTools MCP uses npx" |
| 972 | +新增: fi |
| 973 | +新增: |
| 974 | +新增: printf '\nLanguages\n' >&2 |
| 975 | +新增: printf '─────────\n' >&2 |
| 976 | +新增: install_python=$(_agent_vm_ask_yn "Python 3" Y) |
| 977 | +新增: if [[ -n "$node_forced_reason" ]]; then |
| 978 | +新增: install_node=1 |
| 979 | +新增: printf 'Node.js 24: yes (%s)\n' "$node_forced_reason" >&2 |
| 980 | +新增: else |
| 981 | +新增: install_node=$(_agent_vm_ask_yn "Node.js 24" Y) |
| 982 | +新增: fi |
| 983 | +新增: install_ruby=$(_agent_vm_ask_yn "Ruby" Y) |
| 984 | +新增: install_rust=$(_agent_vm_ask_yn "Rust" Y) |
| 985 | +新增: install_golang=$(_agent_vm_ask_yn "Go" Y) |
| 986 | +新增: fi |
| 987 | +新增: |
| 988 | +新增: # Resources second — same pattern, accept-in-one-go shortcut. Current |
| 989 | +新增: # values reflect any --disk/--memory/--cpus already passed on the CLI. |
| 990 | +新增: # These are starting values: any later `agent-vm` command can resize the |
| 991 | +新增: # per-project VM with --disk/--memory/--cpus. |
| 992 | +新增: printf '\nDefault resources\n' >&2 |
| 993 | +新增: printf '─────────────────\n' >&2 |
| 994 | +新增: printf '(per-VM override with --disk / --memory / --cpus on any agent-vm command)\n\n' >&2 |
| 995 | +新增: printf ' Disk %s GB\n' "$disk" >&2 |
| 996 | +新增: printf ' Memory %s GB\n' "$memory" >&2 |
| 997 | +新增: printf ' CPUs %s\n\n' "$cpus" >&2 |
| 998 | +新增: local use_default_resources |
| 999 | +新增: use_default_resources=$(_agent_vm_ask_yn "Use these defaults" Y) |
| 1000 | +新增: if [[ "$use_default_resources" != "1" ]]; then |
| 1001 | +新增: disk=$(_agent_vm_ask_int "Disk size in GB" "$disk") |
| 1002 | +新增: memory=$(_agent_vm_ask_int "Memory in GB" "$memory") |
| 1003 | +新增: cpus=$(_agent_vm_ask_int "Number of CPUs" "$cpus") |
| 1004 | +新增: fi |
| 1005 | +新增: printf '\n' >&2 |
| 1006 | +新增: fi |
| 1007 | +新增: |
| 1008 | +新增: if [[ "$install_chromium" == "1" ]]; then |
| 1009 | +新增: local wants_chrome_mcp=0 |
| 1010 | +新增: [[ "$install_claude" == "1" || "$install_opencode" == "1" || "$install_codex" == "1" || "$install_vibe" == "1" ]] && wants_chrome_mcp=1 |
| 1011 | +新增: if [[ "$wants_chrome_mcp" == "1" && "$install_node" != "1" ]]; then |
| 1012 | +新增: echo "Enabling Node.js because Chrome DevTools MCP uses npx." >&2 |
| 1013 | +新增: install_node=1 |
| 1014 | +新增: fi |
| 1015 | +新增: fi |
| 1016 | +新增: |
| 1017 | +新增: if [[ "$install_codex" == "1" && "$install_node" != "1" ]]; then |
| 1018 | +新增: echo "Enabling Node.js because Codex CLI requires npm." >&2 |
| 1019 | +新增: install_node=1 |
| 1020 | +新增: fi |
| 1021 | +新增: |
| 1022 | +新增: _agent_vm_clean_partial_state "$AGENT_VM_TEMPLATE" |
| 1023 | +新增: |
| 464 | 1024 | limactl stop "$AGENT_VM_TEMPLATE" &>/dev/null |
| 465 | 1025 | limactl delete "$AGENT_VM_TEMPLATE" --force &>/dev/null |
| 466 | 1026 | |
| @@ -469,19 +1029,55 @@ _agent_vm_setup() { |
| 469 | 1029 | --set '.mounts=[]' |
| 470 | 1030 | --disk="$disk" |
| 471 | 1031 | --memory="$memory" |
| 1032 | +新增: --cpus="$cpus" |
| 472 | 1033 | --tty=false |
| 473 | 1034 | ) |
| 474 | | −删除: [[ -n "$cpus" ]] && create_args+=(--cpus="$cpus") |
| 475 | | −删除: limactl create --name="$AGENT_VM_TEMPLATE" template:debian-13 \ |
| 476 | | −删除: "${create_args[@]}" &>/dev/null || { echo "Error: Failed to create base VM." >&2; return 1; } |
| 1035 | +新增: local create_log |
| 1036 | +新增: if ! create_log=$(limactl create --name="$AGENT_VM_TEMPLATE" template:debian-13 "${create_args[@]}" 2>&1); then |
| 1037 | +新增: echo "Error: Failed to create base VM." >&2 |
| 1038 | +新增: echo "--- limactl create output ---" >&2 |
| 1039 | +新增: echo "$create_log" >&2 |
| 1040 | +新增: return 1 |
| 1041 | +新增: fi |
| 477 | 1042 | |
| 478 | 1043 | _agent_vm_print_resources "$AGENT_VM_TEMPLATE" |
| 479 | 1044 | |
| 480 | | −删除: limactl start "$AGENT_VM_TEMPLATE" &>/dev/null || { echo "Error: Failed to start base VM." >&2; return 1; } |
| 1045 | +新增: local start_log |
| 1046 | +新增: if ! start_log=$(limactl start "$AGENT_VM_TEMPLATE" 2>&1); then |
| 1047 | +新增: echo "Error: Failed to start base VM." >&2 |
| 1048 | +新增: echo "--- limactl start output ---" >&2 |
| 1049 | +新增: echo "$start_log" >&2 |
| 1050 | +新增: echo "Full log: ~/.lima/$AGENT_VM_TEMPLATE/ha.stderr.log" >&2 |
| 1051 | +新增: return 1 |
| 1052 | +新增: fi |
| 481 | 1053 | |
| 482 | | −删除: # Run the setup script inside the VM |
| 1054 | +新增: # Run the setup script inside the VM. Component selections are passed by |
| 1055 | +新增: # prepending `export` lines to the script on stdin — keeps the integration |
| 1056 | +新增: # to one knob (env vars) and avoids quoting headaches with `limactl shell |
| 1057 | +新增: # env KEY=VAL`. The setup script's defaults for each flag match the host |
| 1058 | +新增: # wizard's "default install" set (Ruby/Rust/Go off, everything else on), |
| 1059 | +新增: # so invoking the in-VM script standalone — without these exports — still |
| 1060 | +新增: # produces the same default install. |
| 483 | 1061 | echo "Installing packages inside VM..." |
| 484 | | −删除: limactl shell "$AGENT_VM_TEMPLATE" bash -l < "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" || { echo "Error: Setup script failed." >&2; return 1; } |
| 1062 | +新增: if [[ ! -r "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" ]]; then |
| 1063 | +新增: echo "Error: Setup script not found at ${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" >&2 |
| 1064 | +新增: return 1 |
| 1065 | +新增: fi |
| 1066 | +新增: { |
| 1067 | +新增: printf 'export AGENT_VM_INSTALL_PYTHON=%s\n' "$install_python" |
| 1068 | +新增: printf 'export AGENT_VM_INSTALL_NODE=%s\n' "$install_node" |
| 1069 | +新增: printf 'export AGENT_VM_INSTALL_RUBY=%s\n' "$install_ruby" |
| 1070 | +新增: printf 'export AGENT_VM_INSTALL_RUST=%s\n' "$install_rust" |
| 1071 | +新增: printf 'export AGENT_VM_INSTALL_GOLANG=%s\n' "$install_golang" |
| 1072 | +新增: printf 'export AGENT_VM_INSTALL_DOCKER=%s\n' "$install_docker" |
| 1073 | +新增: printf 'export AGENT_VM_INSTALL_CHROMIUM=%s\n' "$install_chromium" |
| 1074 | +新增: printf 'export AGENT_VM_INSTALL_GH=%s\n' "$install_gh" |
| 1075 | +新增: printf 'export AGENT_VM_INSTALL_CLAUDE=%s\n' "$install_claude" |
| 1076 | +新增: printf 'export AGENT_VM_INSTALL_OPENCODE=%s\n' "$install_opencode" |
| 1077 | +新增: printf 'export AGENT_VM_INSTALL_CODEX=%s\n' "$install_codex" |
| 1078 | +新增: printf 'export AGENT_VM_INSTALL_VIBE=%s\n' "$install_vibe" |
| 1079 | +新增: cat "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" |
| 1080 | +新增: } | limactl shell "$AGENT_VM_TEMPLATE" bash -l || { echo "Error: Setup script failed." >&2; return 1; } |
| 485 | 1081 | |
| 486 | 1082 | # Run user's custom setup script if it exists |
| 487 | 1083 | local user_setup="$AGENT_VM_STATE_DIR/setup.sh" |
| @@ -497,7 +1093,13 @@ _agent_vm_setup() { |
| 497 | 1093 | date +%s > "$AGENT_VM_STATE_DIR/.agent-vm-base-version" |
| 498 | 1094 | |
| 499 | 1095 | echo "" |
| 500 | | −删除: echo "Base VM ready. Run 'agent-vm shell', 'agent-vm claude', 'agent-vm opencode', 'agent-vm codex', or 'agent-vm vibe' in any project directory." |
| 1096 | +新增: echo "Base VM ready. Try one of these in any project directory:" |
| 1097 | +新增: echo " agent-vm shell" |
| 1098 | +新增: [[ "$install_claude" == "1" ]] && echo " agent-vm claude" |
| 1099 | +新增: [[ "$install_opencode" == "1" ]] && echo " agent-vm opencode" |
| 1100 | +新增: [[ "$install_codex" == "1" ]] && echo " agent-vm codex" |
| 1101 | +新增: [[ "$install_vibe" == "1" ]] && echo " agent-vm vibe" |
| 1102 | +新增: echo "" |
| 501 | 1103 | echo "Note: Existing VMs were not updated. Use --reset to re-clone them from the new base." |
| 502 | 1104 | } |
| 503 | 1105 | |
| @@ -527,7 +1129,7 @@ _agent_vm_claude() { |
| 527 | 1129 | _agent_vm_print_resources "$vm_name" |
| 528 | 1130 | |
| 529 | 1131 | local exit_code=0 |
| 530 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" claude --dangerously-skip-permissions "${args[@]}" |
| 1132 | +新增: limactl shell --workdir "$host_dir" "$vm_name" -- claude --dangerously-skip-permissions "${args[@]}" |
| 531 | 1133 | exit_code=$? |
| 532 | 1134 | [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 533 | 1135 | return $exit_code |
| @@ -636,6 +1238,7 @@ _agent_vm_vibe() { |
| 636 | 1238 | _agent_vm_shell() { |
| 637 | 1239 | local vm_opts=() |
| 638 | 1240 | local rm="" |
| 1241 | +新增: local cmd_string="" |
| 639 | 1242 | while [[ $# -gt 0 ]]; do |
| 640 | 1243 | case "$1" in |
| 641 | 1244 | --disk) vm_opts+=(--disk "$2"); shift 2 ;; |
| @@ -646,6 +1249,12 @@ _agent_vm_shell() { |
| 646 | 1249 | --readonly) vm_opts+=(--readonly); shift ;; |
| 647 | 1250 | --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;; |
| 648 | 1251 | --rm) rm=1; shift ;; |
| 1252 | +新增: -c|--command) |
| 1253 | +新增: if [[ $# -lt 2 || -z "$2" ]]; then |
| 1254 | +新增: echo "Error: -c/--command requires a command string." >&2 |
| 1255 | +新增: return 1 |
| 1256 | +新增: fi |
| 1257 | +新增: cmd_string="$2"; shift 2 ;; |
| 649 | 1258 | *) shift ;; |
| 650 | 1259 | esac |
| 651 | 1260 | done |
| @@ -657,15 +1266,22 @@ _agent_vm_shell() { |
| 657 | 1266 | _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 |
| 658 | 1267 | _agent_vm_print_resources "$vm_name" |
| 659 | 1268 | |
| 660 | | −删除: echo "VM: $vm_name | Dir: $host_dir" |
| 661 | | −删除: if [[ -n "$rm" ]]; then |
| 662 | | −删除: echo "Type 'exit' to leave. VM will be destroyed after exit." |
| 1269 | +新增: local exit_code=0 |
| 1270 | +新增: if [[ -n "$cmd_string" ]]; then |
| 1271 | +新增: # One-shot: run the command via a login zsh so ~/.zshenv (mise, PATH, |
| 1272 | +新增: # ~/.agent-vm.env, …) is loaded. No "Type 'exit'..." chatter. |
| 1273 | +新增: limactl shell --workdir "$host_dir" "$vm_name" zsh -l -c "$cmd_string" |
| 1274 | +新增: exit_code=$? |
| 663 | 1275 | else |
| 664 | | −删除: echo "Type 'exit' to leave (VM keeps running). Use 'agent-vm stop' to stop it." |
| 1276 | +新增: echo "VM: $vm_name | Dir: $host_dir" |
| 1277 | +新增: if [[ -n "$rm" ]]; then |
| 1278 | +新增: echo "Type 'exit' to leave. VM will be destroyed after exit." |
| 1279 | +新增: else |
| 1280 | +新增: echo "Type 'exit' to leave (VM keeps running). Use 'agent-vm stop' to stop it." |
| 1281 | +新增: fi |
| 1282 | +新增: limactl shell --workdir "$host_dir" "$vm_name" zsh -l |
| 1283 | +新增: exit_code=$? |
| 665 | 1284 | fi |
| 666 | | −删除: local exit_code=0 |
| 667 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" zsh -l |
| 668 | | −删除: exit_code=$? |
| 669 | 1285 | [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 670 | 1286 | return $exit_code |
| 671 | 1287 | } |
| @@ -674,6 +1290,7 @@ _agent_vm_run() { |
| 674 | 1290 | local vm_opts=() |
| 675 | 1291 | local args=() |
| 676 | 1292 | local rm="" |
| 1293 | +新增: local tty_flag="" |
| 677 | 1294 | while [[ $# -gt 0 ]]; do |
| 678 | 1295 | case "$1" in |
| 679 | 1296 | --disk) vm_opts+=(--disk "$2"); shift 2 ;; |
| @@ -684,11 +1301,12 @@ _agent_vm_run() { |
| 684 | 1301 | --readonly) vm_opts+=(--readonly); shift ;; |
| 685 | 1302 | --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;; |
| 686 | 1303 | --rm) rm=1; shift ;; |
| 1304 | +新增: --tty) tty_flag=1; shift ;; |
| 687 | 1305 | *) args+=("$1"); shift ;; |
| 688 | 1306 | esac |
| 689 | 1307 | done |
| 690 | 1308 | if [[ ${#args[@]} -eq 0 ]]; then |
| 691 | | −删除: echo "Usage: agent-vm run <command> [args]" >&2 |
| 1309 | +新增: echo "Usage: agent-vm run [--tty] <command> [args]" >&2 |
| 692 | 1310 | return 1 |
| 693 | 1311 | fi |
| 694 | 1312 | local host_dir |
| @@ -699,8 +1317,15 @@ _agent_vm_run() { |
| 699 | 1317 | _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 |
| 700 | 1318 | _agent_vm_print_resources "$vm_name" |
| 701 | 1319 | |
| 1320 | +新增: # `--tty` forces limactl to allocate a pseudo-terminal in the VM, which |
| 1321 | +新增: # full-screen TUIs (opencode, vibe, htop, …) need to render correctly when |
| 1322 | +新增: # invoked through `agent-vm run`. Without it, line-mode tools work fine but |
| 1323 | +新增: # ncurses-style UIs break. |
| 1324 | +新增: local shell_opts=(--workdir "$host_dir") |
| 1325 | +新增: [[ -n "$tty_flag" ]] && shell_opts+=(--tty) |
| 1326 | +新增: |
| 702 | 1327 | local exit_code=0 |
| 703 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" "${args[@]}" |
| 1328 | +新增: limactl shell "${shell_opts[@]}" "$vm_name" "${args[@]}" |
| 704 | 1329 | exit_code=$? |
| 705 | 1330 | [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 706 | 1331 | return $exit_code |
| @@ -736,8 +1361,7 @@ _agent_vm_destroy() { |
| 736 | 1361 | echo "Stopping and deleting VM '$vm_name'..." |
| 737 | 1362 | limactl stop "$vm_name" &>/dev/null |
| 738 | 1363 | limactl delete "$vm_name" --force &>/dev/null |
| 739 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 740 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}" |
| 1364 | +新增: _agent_vm_cleanup_state "$vm_name" |
| 741 | 1365 | echo "VM destroyed." |
| 742 | 1366 | } |
| 743 | 1367 | |
| @@ -750,9 +1374,9 @@ _agent_vm_destroy_all() { |
| 750 | 1374 | fi |
| 751 | 1375 | echo "This will destroy the following VMs:" |
| 752 | 1376 | echo "$vms" |
| 753 | | −删除: printf "Continue? [y/N] " |
| 754 | | −删除: local reply |
| 755 | | −删除: read -r reply |
| 1377 | +新增: printf "Continue? [y/N] " >&2 |
| 1378 | +新增: local reply="" |
| 1379 | +新增: IFS= read -r reply </dev/tty 2>/dev/null || reply="" |
| 756 | 1380 | if [[ ! "$reply" =~ ^[Yy]$ ]]; then |
| 757 | 1381 | echo "Aborted." |
| 758 | 1382 | return 0 |
| @@ -761,8 +1385,7 @@ _agent_vm_destroy_all() { |
| 761 | 1385 | echo "Destroying $vm..." |
| 762 | 1386 | limactl stop "$vm" &>/dev/null |
| 763 | 1387 | limactl delete "$vm" --force &>/dev/null |
| 764 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm}" |
| 765 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm}" |
| 1388 | +新增: _agent_vm_cleanup_state "$vm" |
| 766 | 1389 | done |
| 767 | 1390 | echo "All VMs destroyed." |
| 768 | 1391 | } |
| @@ -794,3 +1417,30 @@ _agent_vm_status() { |
| 794 | 1417 | fi |
| 795 | 1418 | done || echo " (no VMs)" |
| 796 | 1419 | } |
| 1420 | +新增: |
| 1421 | +新增:# When the file is executed directly (`./agent-vm.sh setup`) rather than |
| 1422 | +新增:# sourced, dispatch to the agent-vm function so it doesn't silently no-op. |
| 1423 | +新增:# Sourcing remains the canonical install path because it makes `agent-vm` |
| 1424 | +新增:# available as a shell function across all future commands. Detection is |
| 1425 | +新增:# shell-specific: |
| 1426 | +新增:# bash: BASH_SOURCE[0] differs from $0 when sourced |
| 1427 | +新增:# zsh: ZSH_EVAL_CONTEXT contains ':file' when sourced |
| 1428 | +新增:_agent_vm_is_sourced() { |
| 1429 | +新增: if [[ -n "${BASH_VERSION:-}" ]]; then |
| 1430 | +新增: [[ "${BASH_SOURCE[0]}" != "$0" ]] |
| 1431 | +新增: elif [[ -n "${ZSH_VERSION:-}" ]]; then |
| 1432 | +新增: [[ "${ZSH_EVAL_CONTEXT:-}" == *:file* ]] |
| 1433 | +新增: else |
| 1434 | +新增: return 0 # unknown shell — assume sourced and don't auto-run |
| 1435 | +新增: fi |
| 1436 | +新增:} |
| 1437 | +新增: |
| 1438 | +新增:if ! _agent_vm_is_sourced; then |
| 1439 | +新增: unset -f _agent_vm_is_sourced |
| 1440 | +新增: agent-vm "$@" |
| 1441 | +新增: # Preserve agent-vm's exit code — without the explicit exit, the script |
| 1442 | +新增: # would end on the implicit `unset -f` below, which always returns 0 and |
| 1443 | +新增: # would mask command failures (`./agent-vm.sh bad-cmd; echo $?` → 0). |
| 1444 | +新增: exit $? |
| 1445 | +新增:fi |
| 1446 | +新增:unset -f _agent_vm_is_sourced |