| @@ -18,12 +18,47 @@ |
| 18 | 18 | # agent-vm rm - Stop and delete the VM for cwd |
| 19 | 19 | # agent-vm list - List all agent-vm VMs |
| 20 | 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 |
| 21 | 24 | # agent-vm help - Show help |
| 22 | 25 | # |
| 23 | 26 | |
| 27 | +新增:# Semantic version of this file. Bumped by hand on release. Integrators gate on |
| 28 | +新增:# it via `agent-vm version`; a build with no `version` command predates it. |
| 29 | +新增:AGENT_VM_VERSION="0.1.0" |
| 30 | +新增: |
| 24 | 31 | AGENT_VM_TEMPLATE="agent-vm-base" |
| 25 | 32 | AGENT_VM_STATE_DIR="${HOME}/.agent-vm" |
| 26 | | −删除:AGENT_VM_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" |
| 33 | +新增: |
| 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. |
| 49 | +新增:_agent_vm_script_dir() { |
| 50 | +新增: local src="${BASH_SOURCE[0]:-$0}" dir |
| 51 | +新增: while [ -L "$src" ]; do |
| 52 | +新增: dir="$(CDPATH= cd -P -- "$(dirname "$src")" >/dev/null && pwd)" |
| 53 | +新增: src="$(readlink "$src")" |
| 54 | +新增: case "$src" in |
| 55 | +新增: /*) ;; # absolute: use as-is |
| 56 | +新增: *) src="$dir/$src" ;; # relative: to the link's directory |
| 57 | +新增: esac |
| 58 | +新增: done |
| 59 | +新增: (CDPATH= cd -P -- "$(dirname "$src")" >/dev/null && pwd) |
| 60 | +新增:} |
| 61 | +新增:AGENT_VM_SCRIPT_DIR="$(_agent_vm_script_dir)" |
| 27 | 62 | |
| 28 | 63 | # Prompt for a value with a default. Reads from /dev/tty so this still works |
| 29 | 64 | # when called inside command substitution. Writes the prompt to stderr and the |
| @@ -106,7 +141,9 @@ _agent_vm_check_linux_prereqs() { |
| 106 | 141 | errs=1 |
| 107 | 142 | elif [[ ! -r /dev/kvm || ! -w /dev/kvm ]]; then |
| 108 | 143 | 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 |
| 144 | +新增: local groups |
| 145 | +新增: groups="$(id -nG 2>/dev/null || true)" |
| 146 | +新增: if [[ " $groups " != *" kvm "* ]]; then |
| 110 | 147 | echo " Fix: sudo usermod -aG kvm \"\$USER\"" >&2 |
| 111 | 148 | echo " Then log out and back in (or run 'newgrp kvm') so the new" >&2 |
| 112 | 149 | echo " group membership takes effect." >&2 |
| @@ -133,6 +170,33 @@ _agent_vm_clean_partial_state() { |
| 133 | 170 | fi |
| 134 | 171 | } |
| 135 | 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 | +新增: |
| 136 | 200 | # Generate a deterministic VM name for a directory |
| 137 | 201 | _agent_vm_name() { |
| 138 | 202 | local dir="${1:-$(pwd)}" |
| @@ -143,14 +207,71 @@ _agent_vm_name() { |
| 143 | 207 | echo "agent-vm-${base}-${hash}" |
| 144 | 208 | } |
| 145 | 209 | |
| 146 | | −删除:# Check if a VM exists (any state) |
| 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. |
| 147 | 229 | _agent_vm_exists() { |
| 148 | | −删除: limactl list -q 2>/dev/null | grep -q "^${1}$" |
| 230 | +新增: local list |
| 231 | +新增: list="$(limactl list -q 2>/dev/null)" || return 2 |
| 232 | +新增: _agent_vm_has_line "$list" "$1" |
| 149 | 233 | } |
| 150 | 234 | |
| 151 | | −删除:# Check if a VM is running |
| 235 | +新增:# Check if a VM is running. Same three statuses as _agent_vm_exists. |
| 152 | 236 | _agent_vm_running() { |
| 153 | | −删除: limactl list --format '{{.Name}} {{.Status}}' 2>/dev/null | grep -q "^${1} 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 | +新增: fi |
| 154 | 275 | } |
| 155 | 276 | |
| 156 | 277 | # Stage a single host file at <dst> via hardlink, falling back to copy if the |
| @@ -279,20 +400,63 @@ _agent_vm_build_mounts_json() { |
| 279 | 400 | printf '%s' "$mounts_json" |
| 280 | 401 | } |
| 281 | 402 | |
| 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 |
| 419 | +新增:} |
| 420 | +新增: |
| 282 | 421 | # Print VM resource details (CPUs, memory, disk) |
| 283 | 422 | _agent_vm_print_resources() { |
| 284 | | −删除: local vm_name="$1" |
| 285 | | −删除: local info |
| 286 | | −删除: info=$(limactl list --format '{{.Name}}|{{.CPUs}}|{{.Memory}}|{{.Disk}}' 2>/dev/null | grep "^${vm_name}|" | head -1) |
| 287 | | −删除: if [[ -n "$info" ]]; then |
| 288 | | −删除: local cpus mem_bytes disk_bytes |
| 289 | | −删除: IFS='|' read -r _ cpus mem_bytes disk_bytes <<< "$info" |
| 290 | | −删除: local mem_gib=$((mem_bytes / 1073741824)) |
| 291 | | −删除: local disk_gib=$((disk_bytes / 1073741824)) |
| 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" |
| 292 | 426 | echo " Resources: CPUs: ${cpus}, Memory: ${mem_gib} GiB, Disk: ${disk_gib} GiB" |
| 293 | 427 | fi |
| 294 | 428 | } |
| 295 | 429 | |
| 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 |
| 450 | +新增: fi |
| 451 | +新增: if [[ -n "$want_mem" && "$want_mem" != "$cur_mem" ]]; then |
| 452 | +新增: return 0 |
| 453 | +新增: fi |
| 454 | +新增: if [[ -n "$want_disk" && "$want_disk" -gt "$cur_disk" ]]; then |
| 455 | +新增: return 0 |
| 456 | +新增: fi |
| 457 | +新增: return 1 |
| 458 | +新增:} |
| 459 | +新增: |
| 296 | 460 | # Ensure the VM for cwd exists and is running, creating/starting as needed |
| 297 | 461 | # Usage: _agent_vm_ensure_running <vm_name> <host_dir> [--disk GB] [--memory GB] [--reset] |
| 298 | 462 | _agent_vm_ensure_running() { |
| @@ -329,7 +493,7 @@ _agent_vm_ensure_running() { |
| 329 | 493 | # cryptic "no such file or directory". |
| 330 | 494 | _agent_vm_clean_partial_state "$vm_name" |
| 331 | 495 | |
| 332 | | −删除: if ! limactl list -q 2>/dev/null | grep -q "^${AGENT_VM_TEMPLATE}$"; then |
| 496 | +新增: if ! _agent_vm_base_exists; then |
| 333 | 497 | echo "Error: Base VM not found. Run 'agent-vm setup' first." >&2 |
| 334 | 498 | return 1 |
| 335 | 499 | fi |
| @@ -359,7 +523,7 @@ _agent_vm_ensure_running() { |
| 359 | 523 | edit_args+=(--set ".mounts = ${mounts_json}") |
| 360 | 524 | [[ -n "$memory" ]] && edit_args+=(--memory "$memory") |
| 361 | 525 | [[ -n "$cpus" ]] && edit_args+=(--cpus "$cpus") |
| 362 | | −删除: (cd /tmp && limactl edit "$vm_name" "${edit_args[@]}") &>/dev/null |
| 526 | +新增: (cd /tmp && limactl edit "$vm_name" ${edit_args[@]+"${edit_args[@]}"}) &>/dev/null |
| 363 | 527 | if [[ -n "$disk" ]]; then |
| 364 | 528 | if ! (cd /tmp && limactl edit "$vm_name" --disk "$disk") &>/dev/null; then |
| 365 | 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 |
| @@ -371,8 +535,12 @@ _agent_vm_ensure_running() { |
| 371 | 535 | if [[ -f "$base_ver" ]]; then |
| 372 | 536 | cp "$base_ver" "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 373 | 537 | fi |
| 374 | | −删除: elif [[ -n "$disk" || -n "$memory" || -n "$cpus" ]]; then |
| 375 | | −删除: # Auto-resize existing VM if --disk, --memory, or --cpus changed |
| 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. |
| 376 | 544 | if _agent_vm_running "$vm_name"; then |
| 377 | 545 | echo "VM '$vm_name' is currently running. It must be stopped to apply new resource settings." |
| 378 | 546 | printf "Stop the VM and apply changes? [y/N] " >&2 |
| @@ -391,11 +559,16 @@ _agent_vm_ensure_running() { |
| 391 | 559 | local edit_args=() |
| 392 | 560 | [[ -n "$memory" ]] && edit_args+=(--memory "$memory") |
| 393 | 561 | [[ -n "$cpus" ]] && edit_args+=(--cpus "$cpus") |
| 394 | | −删除: local edit_output |
| 395 | | −删除: if ! edit_output=$(cd /tmp && limactl edit "$vm_name" "${edit_args[@]}" 2>&1); then |
| 396 | | −删除: echo "Error: Failed to update VM resources:" >&2 |
| 397 | | −删除: echo "$edit_output" >&2 |
| 398 | | −删除: return 1 |
| 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 |
| 399 | 572 | fi |
| 400 | 573 | if [[ -n "$disk" ]]; then |
| 401 | 574 | if ! edit_output=$(cd /tmp && limactl edit "$vm_name" --disk "$disk" 2>&1); then |
| @@ -405,10 +578,10 @@ _agent_vm_ensure_running() { |
| 405 | 578 | _agent_vm_print_resources "$vm_name" |
| 406 | 579 | fi |
| 407 | 580 | |
| 408 | | −删除: # Warn if this VM was cloned from an older base |
| 409 | | −删除: local base_ver="$AGENT_VM_STATE_DIR/.agent-vm-base-version" |
| 410 | | −删除: local vm_ver="$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 411 | | −删除: if [[ -f "$base_ver" ]] && { [[ ! -f "$vm_ver" ]] || [[ "$(cat "$base_ver")" != "$(cat "$vm_ver")" ]]; }; then |
| 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. |
| 584 | +新增: if [[ "$(_agent_vm_stale_state "$vm_name")" == "1" ]]; then |
| 412 | 585 | echo "Warning: Base VM has been updated since this VM was cloned. Use --reset to re-clone from the new base." >&2 |
| 413 | 586 | fi |
| 414 | 587 | |
| @@ -540,7 +713,7 @@ _agent_vm_ensure_running() { |
| 540 | 713 | # host propagate after a VM restart (ln/cp against the cached staging path). |
| 541 | 714 | # New VMs just staged fresh copies in _agent_vm_build_mounts_json, so there |
| 542 | 715 | # is nothing to refresh. |
| 543 | | −删除: if [[ -z "$is_new_vm" ]]; then |
| 716 | +新增: if [[ -z "$is_new_vm" ]] && [[ ${#file_mount_entries[@]} -gt 0 ]]; then |
| 544 | 717 | local host_src host_staging _bind_src _bind_dst |
| 545 | 718 | for entry in "${file_mount_entries[@]}"; do |
| 546 | 719 | IFS='|' read -r host_src host_staging _bind_src _bind_dst <<< "$entry" |
| @@ -588,20 +761,31 @@ _agent_vm_ensure_running() { |
| 588 | 761 | |
| 589 | 762 | agent-vm() { |
| 590 | 763 | local vm_opts=() |
| 591 | | −删除: # Parse global options before the subcommand |
| 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. |
| 592 | 770 | while [[ $# -gt 0 ]]; do |
| 593 | 771 | case "$1" in |
| 594 | 772 | --disk) |
| 773 | +新增: _agent_vm_validate_int --disk "$2" || return 1 |
| 595 | 774 | vm_opts+=(--disk "$2"); shift 2 ;; |
| 596 | 775 | --disk=*) |
| 776 | +新增: _agent_vm_validate_int --disk "${1#*=}" || return 1 |
| 597 | 777 | vm_opts+=(--disk "${1#*=}"); shift ;; |
| 598 | 778 | --memory|--ram) |
| 779 | +新增: _agent_vm_validate_int --memory "$2" || return 1 |
| 599 | 780 | vm_opts+=(--memory "$2"); shift 2 ;; |
| 600 | 781 | --memory=*|--ram=*) |
| 782 | +新增: _agent_vm_validate_int --memory "${1#*=}" || return 1 |
| 601 | 783 | vm_opts+=(--memory "${1#*=}"); shift ;; |
| 602 | 784 | --cpus) |
| 785 | +新增: _agent_vm_validate_int --cpus "$2" || return 1 |
| 603 | 786 | vm_opts+=(--cpus "$2"); shift 2 ;; |
| 604 | 787 | --cpus=*) |
| 788 | +新增: _agent_vm_validate_int --cpus "${1#*=}" || return 1 |
| 605 | 789 | vm_opts+=(--cpus "${1#*=}"); shift ;; |
| 606 | 790 | --reset) |
| 607 | 791 | vm_opts+=(--reset); shift ;; |
| @@ -625,7 +809,7 @@ agent-vm() { |
| 625 | 809 | # itself; help needs nothing.) Without this, stop/list/status/etc. would fail |
| 626 | 810 | # with confusing empty output instead of a clear, actionable message. |
| 627 | 811 | case "$cmd" in |
| 628 | | −删除: help|--help|-h|setup) ;; |
| 812 | +新增: help|--help|-h|setup|version|--version|-V|name|info|env) ;; |
| 629 | 813 | *) |
| 630 | 814 | if ! command -v limactl &>/dev/null; then |
| 631 | 815 | echo "Error: limactl (Lima) not found. Run 'agent-vm setup' first, or install" >&2 |
| @@ -636,25 +820,25 @@ agent-vm() { |
| 636 | 820 | |
| 637 | 821 | case "$cmd" in |
| 638 | 822 | setup) |
| 639 | | −删除: _agent_vm_setup "${vm_opts[@]}" "$@" |
| 823 | +新增: _agent_vm_setup ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 640 | 824 | ;; |
| 641 | 825 | claude) |
| 642 | | −删除: _agent_vm_claude "${vm_opts[@]}" "$@" |
| 826 | +新增: _agent_vm_claude ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 643 | 827 | ;; |
| 644 | 828 | opencode) |
| 645 | | −删除: _agent_vm_opencode "${vm_opts[@]}" "$@" |
| 829 | +新增: _agent_vm_opencode ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 646 | 830 | ;; |
| 647 | 831 | codex) |
| 648 | | −删除: _agent_vm_codex "${vm_opts[@]}" "$@" |
| 832 | +新增: _agent_vm_codex ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 649 | 833 | ;; |
| 650 | 834 | vibe) |
| 651 | | −删除: _agent_vm_vibe "${vm_opts[@]}" "$@" |
| 835 | +新增: _agent_vm_vibe ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 652 | 836 | ;; |
| 653 | 837 | shell|sh) |
| 654 | | −删除: _agent_vm_shell "${vm_opts[@]}" "$@" |
| 838 | +新增: _agent_vm_shell ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 655 | 839 | ;; |
| 656 | 840 | run) |
| 657 | | −删除: _agent_vm_run "${vm_opts[@]}" "$@" |
| 841 | +新增: _agent_vm_run ${vm_opts[@]+"${vm_opts[@]}"} "$@" |
| 658 | 842 | ;; |
| 659 | 843 | stop) |
| 660 | 844 | _agent_vm_stop "$@" |
| @@ -671,6 +855,22 @@ agent-vm() { |
| 671 | 855 | status) |
| 672 | 856 | _agent_vm_status "$@" |
| 673 | 857 | ;; |
| 858 | +新增: name) |
| 859 | +新增: local name_dir |
| 860 | +新增: name_dir="$(_agent_vm_abs_dir "${1:-}")" || return 1 |
| 861 | +新增: _agent_vm_name "$name_dir" |
| 862 | +新增: ;; |
| 863 | +新增: info) |
| 864 | +新增: local info_dir |
| 865 | +新增: info_dir="$(_agent_vm_abs_dir "${1:-}")" || return 1 |
| 866 | +新增: _agent_vm_info "$info_dir" |
| 867 | +新增: ;; |
| 868 | +新增: env) |
| 869 | +新增: _agent_vm_env "$@" |
| 870 | +新增: ;; |
| 871 | +新增: version|--version|-V) |
| 872 | +新增: echo "$AGENT_VM_VERSION" |
| 873 | +新增: ;; |
| 674 | 874 | help|--help|-h) |
| 675 | 875 | _agent_vm_help |
| 676 | 876 | ;; |
| @@ -682,6 +882,180 @@ agent-vm() { |
| 682 | 882 | esac |
| 683 | 883 | } |
| 684 | 884 | |
| 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 | +新增: |
| 685 | 1059 | _agent_vm_help() { |
| 686 | 1060 | cat << 'EOF' |
| 687 | 1061 | Usage: agent-vm [options] <command> [args] |
| @@ -702,6 +1076,18 @@ Commands: |
| 702 | 1076 | destroy-all Stop and delete all agent-vm VMs |
| 703 | 1077 | list List all agent-vm VMs |
| 704 | 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 |
| 705 | 1091 | help Show this help |
| 706 | 1092 | |
| 707 | 1093 | VM options (for claude, opencode, codex, vibe, shell, run): |
| @@ -763,6 +1149,15 @@ _agent_vm_setup() { |
| 763 | 1149 | local install_ruby=0 install_rust=0 install_golang=0 |
| 764 | 1150 | local install_docker=1 install_chromium=1 install_gh=1 |
| 765 | 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 |
| 766 | 1161 | |
| 767 | 1162 | while [[ $# -gt 0 ]]; do |
| 768 | 1163 | case "$1" in |
| @@ -790,12 +1185,23 @@ Options: |
| 790 | 1185 | 'none' for nothing. |
| 791 | 1186 | Available names: |
| 792 | 1187 | python, node, ruby, rust, golang, docker, chromium, |
| 793 | | −删除: gh, claude, opencode, codex, vibe |
| 1188 | +新增: gh, claude, opencode, codex, vibe, mcp-chrome, |
| 1189 | +新增: mcp-playwright |
| 794 | 1190 | Selecting codex, or chromium with any AI agent, also |
| 795 | 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. |
| 796 | 1201 | Examples: |
| 797 | 1202 | --preinstall=default,rust # default set plus Rust |
| 798 | 1203 | --preinstall=python,docker,claude |
| 1204 | +新增: --preinstall=node,chromium,opencode # no chrome MCP |
| 799 | 1205 | --help Show this help |
| 800 | 1206 | EOF |
| 801 | 1207 | return 0 |
| @@ -868,6 +1274,7 @@ EOF |
| 868 | 1274 | install_python=0 install_node=0 install_ruby=0 install_rust=0 install_golang=0 |
| 869 | 1275 | install_docker=0 install_chromium=0 install_gh=0 |
| 870 | 1276 | install_claude=0 install_opencode=0 install_codex=0 install_vibe=0 |
| 1277 | +新增: install_mcp_chrome=0 install_mcp_playwright=0 |
| 871 | 1278 | [[ -z "$preinstall" ]] && preinstall="default" |
| 872 | 1279 | # Iterate the comma-list portably across bash and zsh by appending a |
| 873 | 1280 | # trailing comma and peeling off one token per iteration. |
| @@ -884,11 +1291,13 @@ EOF |
| 884 | 1291 | install_rust=1 install_golang=1 |
| 885 | 1292 | install_docker=1 install_chromium=1 install_gh=1 |
| 886 | 1293 | install_claude=1 install_opencode=1 install_codex=1 install_vibe=1 |
| 1294 | +新增: install_mcp_chrome=1 install_mcp_playwright=1 |
| 887 | 1295 | ;; |
| 888 | 1296 | default) |
| 889 | 1297 | install_python=1 install_node=1 |
| 890 | 1298 | install_docker=1 install_chromium=1 install_gh=1 |
| 891 | 1299 | install_claude=1 install_opencode=1 install_codex=1 install_vibe=1 |
| 1300 | +新增: install_mcp_chrome=1 |
| 892 | 1301 | ;; |
| 893 | 1302 | none) ;; # explicit no-op token; with the all-off reset above, |
| 894 | 1303 | # `--preinstall=none` ships nothing. |
| @@ -904,9 +1313,11 @@ EOF |
| 904 | 1313 | opencode) install_opencode=1 ;; |
| 905 | 1314 | codex) install_codex=1 ;; |
| 906 | 1315 | vibe) install_vibe=1 ;; |
| 1316 | +新增: mcp-chrome) install_mcp_chrome=1 ;; |
| 1317 | +新增: mcp-playwright) install_mcp_playwright=1 ;; |
| 907 | 1318 | *) |
| 908 | 1319 | 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 |
| 1320 | +新增: echo "Valid: python, node, ruby, rust, golang, docker, chromium, gh, claude, opencode, codex, vibe, mcp-chrome, mcp-playwright, default, all, none" >&2 |
| 910 | 1321 | return 1 |
| 911 | 1322 | ;; |
| 912 | 1323 | esac |
| @@ -946,8 +1357,9 @@ EOF |
| 946 | 1357 | printf 'Software\n' >&2 |
| 947 | 1358 | printf '────────\n' >&2 |
| 948 | 1359 | 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 |
| 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 |
| 951 | 1363 | local use_default_software |
| 952 | 1364 | use_default_software=$(_agent_vm_ask_yn "Use this default" Y) |
| 953 | 1365 | if [[ "$use_default_software" != "1" ]]; then |
| @@ -964,10 +1376,24 @@ EOF |
| 964 | 1376 | install_chromium=$(_agent_vm_ask_yn "Chromium (headless browser)" Y) |
| 965 | 1377 | install_gh=$(_agent_vm_ask_yn "GitHub CLI (gh)" Y) |
| 966 | 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 | +新增: |
| 967 | 1393 | local node_forced_reason="" |
| 968 | 1394 | if [[ "$install_codex" == "1" ]]; then |
| 969 | 1395 | node_forced_reason="Codex CLI requires Node.js" |
| 970 | | −删除: elif [[ "$install_chromium" == "1" && ( "$install_claude" == "1" || "$install_opencode" == "1" || "$install_vibe" == "1" ) ]]; then |
| 1396 | +新增: elif [[ "$install_chromium" == "1" && "$install_mcp_chrome" == "1" && ( "$install_claude" == "1" || "$install_opencode" == "1" || "$install_vibe" == "1" ) ]]; then |
| 971 | 1397 | node_forced_reason="Chrome DevTools MCP uses npx" |
| 972 | 1398 | fi |
| 973 | 1399 | |
| @@ -1005,7 +1431,7 @@ EOF |
| 1005 | 1431 | printf '\n' >&2 |
| 1006 | 1432 | fi |
| 1007 | 1433 | |
| 1008 | | −删除: if [[ "$install_chromium" == "1" ]]; then |
| 1434 | +新增: if [[ "$install_chromium" == "1" && "$install_mcp_chrome" == "1" ]]; then |
| 1009 | 1435 | local wants_chrome_mcp=0 |
| 1010 | 1436 | [[ "$install_claude" == "1" || "$install_opencode" == "1" || "$install_codex" == "1" || "$install_vibe" == "1" ]] && wants_chrome_mcp=1 |
| 1011 | 1437 | if [[ "$wants_chrome_mcp" == "1" && "$install_node" != "1" ]]; then |
| @@ -1076,6 +1502,8 @@ EOF |
| 1076 | 1502 | printf 'export AGENT_VM_INSTALL_OPENCODE=%s\n' "$install_opencode" |
| 1077 | 1503 | printf 'export AGENT_VM_INSTALL_CODEX=%s\n' "$install_codex" |
| 1078 | 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" |
| 1079 | 1507 | cat "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" |
| 1080 | 1508 | } | limactl shell "$AGENT_VM_TEMPLATE" bash -l || { echo "Error: Setup script failed." >&2; return 1; } |
| 1081 | 1509 | |
| @@ -1148,11 +1576,11 @@ _agent_vm_claude() { |
| 1148 | 1576 | local vm_name |
| 1149 | 1577 | vm_name="$(_agent_vm_name "$host_dir")" |
| 1150 | 1578 | |
| 1151 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 |
| 1579 | +新增: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1152 | 1580 | _agent_vm_print_resources "$vm_name" |
| 1153 | 1581 | |
| 1154 | 1582 | local exit_code=0 |
| 1155 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" "" claude --dangerously-skip-permissions "${args[@]}" |
| 1583 | +新增: _agent_vm_lima_run "$vm_name" "$host_dir" "" claude --dangerously-skip-permissions ${args[@]+"${args[@]}"} |
| 1156 | 1584 | exit_code=$? |
| 1157 | 1585 | [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1158 | 1586 | return $exit_code |
| @@ -1180,14 +1608,14 @@ _agent_vm_opencode() { |
| 1180 | 1608 | local vm_name |
| 1181 | 1609 | vm_name="$(_agent_vm_name "$host_dir")" |
| 1182 | 1610 | |
| 1183 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 |
| 1611 | +新增: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1184 | 1612 | _agent_vm_print_resources "$vm_name" |
| 1185 | 1613 | |
| 1186 | 1614 | # --auto auto-approves permission prompts that aren't explicitly denied, |
| 1187 | 1615 | # giving full autonomy (safe inside the sandbox). This is OpenCode's shipped |
| 1188 | 1616 | # equivalent of a "yolo" mode; the proposed --yolo flag was never merged. |
| 1189 | 1617 | local exit_code=0 |
| 1190 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" 1 opencode --auto "${args[@]}" |
| 1618 | +新增: _agent_vm_lima_run "$vm_name" "$host_dir" 1 opencode --auto ${args[@]+"${args[@]}"} |
| 1191 | 1619 | exit_code=$? |
| 1192 | 1620 | [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1193 | 1621 | return $exit_code |
| @@ -1215,11 +1643,11 @@ _agent_vm_codex() { |
| 1215 | 1643 | local vm_name |
| 1216 | 1644 | vm_name="$(_agent_vm_name "$host_dir")" |
| 1217 | 1645 | |
| 1218 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 |
| 1646 | +新增: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1219 | 1647 | _agent_vm_print_resources "$vm_name" |
| 1220 | 1648 | |
| 1221 | 1649 | local exit_code=0 |
| 1222 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" "" codex --dangerously-bypass-approvals-and-sandbox "${args[@]}" |
| 1650 | +新增: _agent_vm_lima_run "$vm_name" "$host_dir" "" codex --dangerously-bypass-approvals-and-sandbox ${args[@]+"${args[@]}"} |
| 1223 | 1651 | exit_code=$? |
| 1224 | 1652 | [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1225 | 1653 | return $exit_code |
| @@ -1247,13 +1675,13 @@ _agent_vm_vibe() { |
| 1247 | 1675 | local vm_name |
| 1248 | 1676 | vm_name="$(_agent_vm_name "$host_dir")" |
| 1249 | 1677 | |
| 1250 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 |
| 1678 | +新增: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1251 | 1679 | _agent_vm_print_resources "$vm_name" |
| 1252 | 1680 | |
| 1253 | 1681 | # Vibe is a full-screen TUI, so allocate a tty (like opencode). |
| 1254 | 1682 | # --agent auto-approve gives full autonomy (safe inside the sandbox). |
| 1255 | 1683 | local exit_code=0 |
| 1256 | | −删除: _agent_vm_lima_run "$vm_name" "$host_dir" 1 vibe --agent auto-approve "${args[@]}" |
| 1684 | +新增: _agent_vm_lima_run "$vm_name" "$host_dir" 1 vibe --agent auto-approve ${args[@]+"${args[@]}"} |
| 1257 | 1685 | exit_code=$? |
| 1258 | 1686 | [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; } |
| 1259 | 1687 | return $exit_code |
| @@ -1287,7 +1715,7 @@ _agent_vm_shell() { |
| 1287 | 1715 | local vm_name |
| 1288 | 1716 | vm_name="$(_agent_vm_name "$host_dir")" |
| 1289 | 1717 | |
| 1290 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 |
| 1718 | +新增: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1291 | 1719 | _agent_vm_print_resources "$vm_name" |
| 1292 | 1720 | |
| 1293 | 1721 | local exit_code=0 |
| @@ -1338,7 +1766,7 @@ _agent_vm_run() { |
| 1338 | 1766 | local vm_name |
| 1339 | 1767 | vm_name="$(_agent_vm_name "$host_dir")" |
| 1340 | 1768 | |
| 1341 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 |
| 1769 | +新增: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 1342 | 1770 | _agent_vm_print_resources "$vm_name" |
| 1343 | 1771 | |
| 1344 | 1772 | # `--tty` forces limactl to allocate a pseudo-terminal in the VM, which |