| @@ -1,1219 +1,56 @@ |
| 1 | 1 | #!/usr/bin/env bash |
| 2 | 2 | # |
| 3 | | −删除:# agent-vm: Run AI coding agents inside sandboxed Lima VMs |
| 4 | | −删除:# Part of https://www.agent-vm.org/ |
| 5 | | −删除:# |
| 6 | | −删除:# Put it on your PATH with `./agent-vm.sh install`. It can also be sourced |
| 7 | | −删除:# from bash, which defines agent-vm as a shell function. Commands: |
| 8 | | −删除:# `agent-vm help` (lib/help.sh). |
| 9 | | −删除: |
| 10 | | −删除:# zsh, which 0.1.0's installer had source this file from ~/.zshrc: only a |
| 11 | | −删除:# function that runs this file with bash. %x is the file being sourced, :A |
| 12 | | −删除:# makes it absolute with links resolved. The eval keeps bash from parsing zsh |
| 13 | | −删除:# syntax. |
| 14 | | −删除:if [[ -n "${ZSH_VERSION:-}" ]]; then |
| 15 | | −删除: eval ' |
| 16 | | −删除: [[ "${ZSH_EVAL_CONTEXT:-}" == *:file* ]] || exec bash "$0" "$@" |
| 17 | | −删除: eval "agent-vm() { command bash ${(qq)${(%):-%x}:A} \"\$@\"; }" |
| 18 | | −删除: ' |
| 3 | +新增:# Runs agent-vm: in a release, the binary next to this script; in a clone, |
| 4 | +新增:# the one `make` builds, built first when missing or when HEAD moved (a git |
| 5 | +新增:# pull). `agent-vm.sh install` links the binary onto the PATH; the curl |
| 6 | +新增:# installer runs it so. |
| 7 | +新增:# |
| 8 | +新增:# What 0.2 left behind keeps working through it: a link to this file on the |
| 9 | +新增:# PATH (0.2's install), and a shell rc that sources it (0.1 and 0.2), which |
| 10 | +新增:# gets an agent-vm function running it. |
| 11 | +新增: |
| 12 | +新增:# Sourced: nothing below runs in the user's shell, where set -e or exec would |
| 13 | +新增:# end it. |
| 14 | +新增:if [ -n "${ZSH_VERSION:-}" ]; then |
| 15 | +新增: case "${ZSH_EVAL_CONTEXT:-}" in |
| 16 | +新增: *:file*) |
| 17 | +新增: eval '_agent_vm_sh=${(%):-%x}' |
| 18 | +新增: _agent_vm_sh="$(cd -P -- "$(dirname -- "$_agent_vm_sh")" >/dev/null && pwd)/$(basename -- "$_agent_vm_sh")" |
| 19 | +新增: agent-vm() { bash "$_agent_vm_sh" "$@"; } |
| 20 | +新增: return 0 ;; |
| 21 | +新增: esac |
| 22 | +新增:elif [ -n "${BASH_VERSION:-}" ] && [ "${BASH_SOURCE[0]}" != "$0" ]; then |
| 23 | +新增: _agent_vm_sh="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" >/dev/null && pwd)/$(basename -- "${BASH_SOURCE[0]}")" |
| 24 | +新增: agent-vm() { bash "$_agent_vm_sh" "$@"; } |
| 19 | 25 | return 0 |
| 20 | 26 | fi |
| 21 | 27 | |
| 22 | | −删除:# Semantic version of this file. Bumped by hand on release. Integrators gate on |
| 23 | | −删除:# it via `agent-vm version`; a build with no `version` command predates it. |
| 24 | | −删除:AGENT_VM_VERSION="0.2.1" |
| 25 | | −删除: |
| 26 | | −删除:AGENT_VM_TEMPLATE="agent-vm-base" |
| 27 | | −删除:# Overridable, for a test or a second install, without moving HOME (and |
| 28 | | −删除:# Lima's state with it). `agent-vm info` publishes it as state_dir=. |
| 29 | | −删除:AGENT_VM_STATE_DIR="${AGENT_VM_STATE_DIR:-${HOME}/.agent-vm}" |
| 30 | | −删除: |
| 31 | | −删除:# Directory of the real agent-vm.sh, symlinks followed: the PATH entry |
| 32 | | −删除:# `install` makes is one. By hand, as macOS's readlink has no -f. `CDPATH=` |
| 33 | | −删除:# everywhere a relative path is cd'ed to: with it set, cd searches it first. |
| 34 | | −删除:_agent_vm_script_dir() { |
| 35 | | −删除: local src="$1" dir n=0 |
| 36 | | −删除: # At most 40 links, as the kernel follows: a loop would never end. |
| 37 | | −删除: while [ -L "$src" ] && [ "$n" -lt 40 ]; do |
| 38 | | −删除: n=$((n + 1)) |
| 39 | | −删除: dir="$(CDPATH= cd -P -- "$(dirname "$src")" >/dev/null && pwd)" |
| 40 | | −删除: src="$(readlink "$src")" |
| 41 | | −删除: case "$src" in |
| 42 | | −删除: /*) ;; # absolute: use as-is |
| 43 | | −删除: *) src="$dir/$src" ;; # relative: to the link's directory |
| 44 | | −删除: esac |
| 45 | | −删除: done |
| 46 | | −删除: (CDPATH= cd -P -- "$(dirname "$src")" >/dev/null && pwd) |
| 47 | | −删除:} |
| 48 | | −删除:AGENT_VM_SCRIPT_DIR="$(_agent_vm_script_dir "${BASH_SOURCE[0]:-$0}")" |
| 49 | | −删除:if [[ -z "$AGENT_VM_SCRIPT_DIR" ]]; then |
| 50 | | −删除: echo "agent-vm: cannot find the directory of agent-vm.sh" >&2 |
| 51 | | −删除: return 1 2>/dev/null || exit 1 |
| 52 | | −删除:fi |
| 28 | +新增:set -euo pipefail |
| 53 | 29 | |
| 54 | | −删除:# The rest of agent-vm, one file per concern, sourced like this one. Only |
| 55 | | −删除:# functions and settings: nothing runs until a command does. A file that is |
| 56 | | −删除:# missing stops here, not half-way through a command. |
| 57 | | −删除:for _agent_vm_lib in ui options host vm mounts git runtime env info install doctor help setup code; do |
| 58 | | −删除: _agent_vm_lib="$AGENT_VM_SCRIPT_DIR/lib/$_agent_vm_lib.sh" |
| 59 | | −删除: if [[ ! -r "$_agent_vm_lib" ]] || ! . "$_agent_vm_lib"; then |
| 60 | | −删除: echo "agent-vm: cannot load $_agent_vm_lib" >&2 |
| 61 | | −删除: unset _agent_vm_lib |
| 62 | | −删除: return 1 2>/dev/null || exit 1 |
| 63 | | −删除: fi |
| 30 | +新增:# This file's folder, through the links to it (0.2's on the PATH). |
| 31 | +新增:src="${BASH_SOURCE[0]:-$0}" |
| 32 | +新增:for _ in 1 2 3 4 5 6 7 8 9 10; do |
| 33 | +新增: [ -L "$src" ] || break |
| 34 | +新增: t="$(readlink "$src")" |
| 35 | +新增: case "$t" in /*) src="$t" ;; *) src="$(dirname "$src")/$t" ;; esac |
| 64 | 36 | done |
| 65 | | −删除:unset _agent_vm_lib |
| 66 | | −删除: |
| 67 | | −删除:# --- starting a project's VM -------------------------------------------------- |
| 68 | | −删除: |
| 69 | | −删除:# The security checks of a start, for the project <dir> and its VM <vm>. Reads the caller's |
| 70 | | −删除:# vm_up, scratch and rdonly, and sets its protect_git, ro_names and |
| 71 | | −删除:# hooks_notes. With vm_up set (a running VM that keeps running), the risks are |
| 72 | | −删除:# printed and nothing is asked. Fails when the start must stop. |
| 73 | | −删除:_agent_vm_start_checks() { |
| 74 | | −删除: local host_dir="$1" vm_name="$2" lima_st=0 |
| 75 | | −删除: protect_git="" ro_names="" hooks_notes="" |
| 76 | | −删除: |
| 77 | | −删除: # Every .git read-only for the guest, when this Lima can enforce it and it |
| 78 | | −删除: # was not turned off. The mounts built afterwards, on every path, follow the |
| 79 | | −删除: # answer. A Lima that cannot, with shares left writable and nobody having |
| 80 | | −删除: # asked for a writable .git, is a risk to accept. One whose answer could not |
| 81 | | −删除: # be read stops the start: taking it for a no would drop the protection of |
| 82 | | −删除: # the VM. |
| 83 | | −删除: if [[ -n "$scratch" ]]; then |
| 84 | | −删除: : |
| 85 | | −删除: elif _agent_vm_writable_git_optout; then |
| 86 | | −删除: _agent_vm_writable_git_warning >&2 |
| 87 | | −删除: else |
| 88 | | −删除: _agent_vm_lima_protects_git || lima_st=$? |
| 89 | | −删除: if [[ "$lima_st" == 0 ]]; then |
| 90 | | −删除: protect_git=1 |
| 91 | | −删除: elif [[ -n "$rdonly" ]]; then |
| 92 | | −删除: : |
| 93 | | −删除: elif [[ "$lima_st" == 2 ]]; then |
| 94 | | −删除: echo "Error: cannot tell whether this Lima keeps .git read-only: 'limactl validate' gave no answer agent-vm knows ('agent-vm doctor' shows more)." >&2 |
| 95 | | −删除: echo " --readonly, or --unsafe-writable-git to accept a writable .git, still starts the VM." >&2 |
| 96 | | −删除: return 1 |
| 97 | | −删除: elif _agent_vm_unprotected_mount_is_sshfs "$vm_name"; then |
| 98 | | −删除: # Asked below, with the rest of the disk. |
| 99 | | −删除: : |
| 100 | | −删除: elif [[ -n "$vm_up" ]]; then |
| 101 | | −删除: echo "Warning: this Lima cannot keep .git read-only, so the VM can write .git ('agent-vm doctor' says more)." >&2 |
| 102 | | −删除: else |
| 103 | | −删除: _agent_vm_git_protection_hint | _agent_vm_box "Lima cannot keep .git read-only" |
| 104 | | −删除: if ! _agent_vm_confirm_unsafe; then |
| 105 | | −删除: echo "Aborted. Install that Lima build, or use --readonly; --unsafe-writable-git accepts a writable .git." >&2 |
| 106 | | −删除: return 1 |
| 107 | | −删除: fi |
| 108 | | −删除: fi |
| 109 | | −删除: fi |
| 110 | | −删除: |
| 111 | | −删除: # Not only .git: shares Lima does not confine leave the rest of the disk |
| 112 | | −删除: # in reach (see _agent_vm_unprotected_mount_is_sshfs), --unsafe-writable-git |
| 113 | | −删除: # or not. --readonly cannot be enforced on them either: refused before |
| 114 | | −删除: # anything boots. |
| 115 | | −删除: if [[ -z "$scratch" && -z "$protect_git" ]] && _agent_vm_unprotected_mount_is_sshfs "$vm_name"; then |
| 116 | | −删除: if [[ -n "$rdonly" ]]; then |
| 117 | | −删除: echo "Error: --readonly cannot be enforced here: the shares would be reverse-sshfs, which this Lima does not confine. Install the Lima build 'agent-vm setup' offers, or use --scratch." >&2 |
| 118 | | −删除: return 1 |
| 119 | | −删除: elif [[ -n "$vm_up" ]]; then |
| 120 | | −删除: echo "Warning: this Lima cannot keep the VM to its shares: root in the VM may reach files outside them, your SSH keys included ('agent-vm doctor' says more)." >&2 |
| 121 | | −删除: else |
| 122 | | −删除: { _agent_vm_sshfs_exposure_note; _agent_vm_git_protection_hint; } | _agent_vm_box "Lima cannot keep the VM to its shares" |
| 123 | | −删除: if ! _agent_vm_confirm_unsafe; then |
| 124 | | −删除: echo "Aborted. Install that Lima build, or use --scratch, which shares nothing." >&2 |
| 125 | | −删除: return 1 |
| 126 | | −删除: fi |
| 127 | | −删除: fi |
| 128 | | −删除: fi |
| 129 | | −删除: |
| 130 | | −删除: # With it, the names the shares need: those always listed, and one per |
| 131 | | −删除: # hooks folder git runs from in a share (see _agent_vm_git_scan), its |
| 132 | | −删除: # first component. A dot-name (.husky, .githooks) is taken as is; any |
| 133 | | −删除: # other is read-only in every folder of every share, so it is asked first |
| 134 | | −删除: # (yes by default, and when no one can answer). A folder that cannot be |
| 135 | | −删除: # protected, or one declined, is a risk to accept. So are the scan's risks: |
| 136 | | −删除: # git config files in a share, settings running commands from one, hooks |
| 137 | | −删除: # linked into one. Under --readonly the VM writes none of them: the names |
| 138 | | −删除: # are taken as they come, with no warning and no question. |
| 139 | | −删除: local hooks_rel hooks_in hooks_name hooks_names=() scan="" |
| 140 | | −删除: if [[ -n "$protect_git" ]]; then |
| 141 | | −删除: scan="$(_agent_vm_git_scan "$host_dir")" |
| 142 | | −删除: while IFS=$'\t' read -r hooks_rel hooks_in; do |
| 143 | | −删除: [[ -n "$hooks_rel" ]] || continue |
| 144 | | −删除: if [[ -n "$rdonly" ]]; then |
| 145 | | −删除: hooks_name="$(_agent_vm_hooks_name "$hooks_in")" && hooks_names+=("$hooks_name") |
| 146 | | −删除: continue |
| 147 | | −删除: fi |
| 148 | | −删除: if ! hooks_name="$(_agent_vm_hooks_name "$hooks_in")"; then |
| 149 | | −删除: if [[ "$hooks_rel" == . ]]; then |
| 150 | | −删除: echo "Warning: git's core.hooksPath is the project directory itself, which the VM can write: git on this machine runs the hooks the agent puts there." >&2 |
| 151 | | −删除: elif [[ "$hooks_in" == . ]]; then |
| 152 | | −删除: echo "Warning: git's core.hooksPath is '$hooks_rel', a writable volume itself: git on this machine runs the hooks the agent puts there." >&2 |
| 153 | | −删除: else |
| 154 | | −删除: echo "Warning: git's core.hooksPath is '$hooks_rel', which the VM can write, and its name cannot be made read-only: git on this machine runs the hooks the agent puts there." >&2 |
| 155 | | −删除: fi |
| 156 | | −删除: if [[ -z "$vm_up" ]] && ! _agent_vm_confirm_unsafe; then |
| 157 | | −删除: echo "Aborted. Point core.hooksPath to a folder of the project (agent-vm keeps it read-only), or outside it." >&2 |
| 158 | | −删除: return 1 |
| 159 | | −删除: fi |
| 160 | | −删除: continue |
| 161 | | −删除: fi |
| 162 | | −删除: if [[ "$hooks_name" != .* && -z "$vm_up" ]] && ! _agent_vm_prompts_disabled_by >/dev/null \ |
| 163 | | −删除: && _agent_vm_can_ask \ |
| 164 | | −删除: && [[ "$(_agent_vm_ask_yn "Git runs hooks from $hooks_rel. Make every '$hooks_name' folder in the project read-only for the VM?" Y)" != "1" ]]; then |
| 165 | | −删除: echo "Warning: '$hooks_name' stays writable, and git on this machine runs the hooks the agent puts in $hooks_rel." >&2 |
| 166 | | −删除: if ! _agent_vm_confirm_unsafe; then |
| 167 | | −删除: echo "Aborted. Answer yes to protect it, or point core.hooksPath outside the project." >&2 |
| 168 | | −删除: return 1 |
| 169 | | −删除: fi |
| 170 | | −删除: continue |
| 171 | | −删除: fi |
| 172 | | −删除: hooks_names+=("$hooks_name") |
| 173 | | −删除: hooks_notes="$hooks_notes$(_agent_vm_hooks_note "$hooks_rel" "$hooks_name")"$'\n' |
| 174 | | −删除: done <<< "$(_agent_vm_scan_part H "$scan")" |
| 175 | | −删除: ro_names="$(_agent_vm_names_json ${hooks_names[@]+"${hooks_names[@]}"})" |
| 176 | | −删除: |
| 177 | | −删除: local git_risks="" |
| 178 | | −删除: [[ -n "$rdonly" ]] || git_risks="$(_agent_vm_scan_part R "$scan")" |
| 179 | | −删除: if [[ -n "$git_risks" ]]; then |
| 180 | | −删除: echo "Warning: git on this machine uses these, and the VM can write them:" >&2 |
| 181 | | −删除: printf '%s\n' "$git_risks" | sed 's/^/ /' >&2 |
| 182 | | −删除: if [[ -z "$vm_up" ]] && ! _agent_vm_confirm_unsafe; then |
| 183 | | −删除: echo "Aborted. Move them out of the shared folders, or have them name commands outside them." >&2 |
| 184 | | −删除: return 1 |
| 185 | | −删除: fi |
| 186 | | −删除: fi |
| 187 | | −删除: fi |
| 188 | | −删除: # A repository not named .git, which no name protects (see |
| 189 | | −删除: # _agent_vm_bare_repo_state): only a VM that can write needs the setting. |
| 190 | | −删除: if [[ -z "$rdonly" && -z "$scratch" ]]; then |
| 191 | | −删除: _agent_vm_check_bare_repo_setting "$vm_up" || return 1 |
| 192 | | −删除: fi |
| 193 | | −删除:} |
| 194 | | −删除: |
| 195 | | −删除:# A VM that ran when the start began, so whose start asked nothing (see |
| 196 | | −删除:# _agent_vm_start_checks), has just been stopped to boot again: ask now, as |
| 197 | | −删除:# for any VM about to boot. Reads and clears the caller's vm_up. Fails, with |
| 198 | | −删除:# the VM left stopped, when the start must stop. |
| 199 | | −删除:_agent_vm_recheck_after_stop() { |
| 200 | | −删除: [[ -n "$vm_up" ]] || return 0 |
| 201 | | −删除: vm_up="" |
| 202 | | −删除: if ! _agent_vm_start_checks "$1" "$2"; then |
| 203 | | −删除: echo "VM '$2' is stopped." >&2 |
| 204 | | −删除: return 1 |
| 205 | | −删除: fi |
| 206 | | −删除:} |
| 207 | | −删除: |
| 208 | | −删除:# Stops <vm>, for the project <dir>, to boot it again changed, then asks what |
| 209 | | −删除:# its start did not (see above). Fails, saying so, when it did not stop. |
| 210 | | −删除:_agent_vm_stop_to_change() { |
| 211 | | −删除: if ! _agent_vm_stop_vm "$1"; then |
| 212 | | −删除: echo "Error: could not stop VM '$1': nothing was changed." >&2 |
| 213 | | −删除: return 1 |
| 214 | | −删除: fi |
| 215 | | −删除: _agent_vm_recheck_after_stop "$2" "$1" |
| 216 | | −删除:} |
| 217 | | −删除: |
| 218 | | −删除:# Gives the stopped <vm> its shares for the project <dir>, writable or not |
| 219 | | −删除:# (<writable> true/false), with the caller's protect_git and ro_names, or none |
| 220 | | −删除:# for its scratch, and records them (see _agent_vm_record_mounts). |
| 221 | | −删除:_agent_vm_apply_shares() { |
| 222 | | −删除: local vm="$1" dir="$2" writable="$3" json="[]" out |
| 223 | | −删除: [[ -n "$scratch" ]] || json="$(_agent_vm_build_mounts_json "$vm" "$dir" "$writable" "${protect_git:+$ro_names}")" |
| 224 | | −删除: if ! out=$(cd /tmp && limactl edit "$vm" --set "$(_agent_vm_mounts_expr "$json" "$protect_git")" 2>&1); then |
| 225 | | −删除: echo "Error: could not set the shares of VM '$vm':" >&2 |
| 226 | | −删除: echo "$out" >&2 |
| 227 | | −删除: return 1 |
| 228 | | −删除: fi |
| 229 | | −删除: _agent_vm_record_mounts "$vm" "$json" || return 1 |
| 230 | | −删除: _agent_vm_sshfs_cache_mode >| "$AGENT_VM_STATE_DIR/.agent-vm-sshfs-cache-$vm" |
| 231 | | −删除:} |
| 232 | | −删除: |
| 233 | | −删除:# Before the stopped <vm> boots, fails, saying why, unless what Lima's config |
| 234 | | −删除:# gives it is what agent-vm set: for a VM whose .git is protected (the |
| 235 | | −删除:# caller's protect_git and ro_names), every share with the read-only names |
| 236 | | −删除:# (see _agent_vm_live_shares_why); for one that shares nothing (the caller's |
| 237 | | −删除:# scratch, or <none> set), no share at all. Lima merges the mounts of its |
| 238 | | −删除:# _config/default.yaml and override.yaml into every VM, and agent-vm's edit |
| 239 | | −删除:# cannot remove them. |
| 240 | | −删除:_agent_vm_check_before_start() { |
| 241 | | −删除: # shellcheck disable=SC2034 # set by _agent_vm_live_fields, all three |
| 242 | | −删除: local vm="$1" none="${2:-$scratch}" live why cfg live_vmtype live_mtype live_json |
| 243 | | −删除: [[ -n "$protect_git" || -n "$none" ]] || return 0 |
| 244 | | −删除: cfg="$(_agent_vm_lima_home)/_config" |
| 245 | | −删除: if ! live="$(_agent_vm_live_config "$vm")" || ! _agent_vm_live_fields "$live"; then |
| 246 | | −删除: echo "Error: could not read Lima's config for VM '$vm', so what it would boot with is not known." >&2 |
| 247 | | −删除: return 1 |
| 248 | | −删除: fi |
| 249 | | −删除: if [[ -n "$none" ]]; then |
| 250 | | −删除: [[ "$live_json" == null || "$live_json" == "[]" ]] && return 0 |
| 251 | | −删除: echo "Error: Lima gives VM '$vm' shares agent-vm did not ask for, so it would not boot with nothing of yours." >&2 |
| 252 | | −删除: echo " Look for 'mounts' in $cfg/override.yaml and default.yaml, and remove them." >&2 |
| 253 | | −删除: return 1 |
| 254 | | −删除: fi |
| 255 | | −删除: why="$(_agent_vm_live_shares_why "$live" "$ro_names")" && return 0 |
| 256 | | −删除: echo "Error: VM '$vm' would boot with .git writable: $why." >&2 |
| 257 | | −删除: case "$why" in |
| 258 | | −删除: *"mount type"*) echo " Look for 'mountType' in $cfg/override.yaml and default.yaml, and remove it." >&2 ;; |
| 259 | | −删除: *) echo " Look for 'mounts' in $cfg/override.yaml and default.yaml, and remove them." >&2 ;; |
| 260 | | −删除: esac |
| 261 | | −删除: return 1 |
| 262 | | −删除:} |
| 263 | | −删除: |
| 264 | | −删除:# The caller's memory, cpus, ssh_port and disk, on the stopped <vm>. The disk |
| 265 | | −删除:# apart, with a warning only: Lima rejects a whole edit that would shrink it. |
| 266 | | −删除:_agent_vm_apply_resources() { |
| 267 | | −删除: local vm="$1" out args=() |
| 268 | | −删除: [[ -z "$memory" ]] || args+=(--memory "$memory") |
| 269 | | −删除: [[ -z "$cpus" ]] || args+=(--cpus "$cpus") |
| 270 | | −删除: [[ -z "$ssh_port" ]] || args+=(--set ".ssh.localPort = $ssh_port") |
| 271 | | −删除: # Never without arguments: `limactl edit <vm>` alone opens $EDITOR. |
| 272 | | −删除: if [[ ${#args[@]} -gt 0 ]] && ! out=$(cd /tmp && limactl edit "$vm" "${args[@]}" 2>&1); then |
| 273 | | −删除: echo "Error: could not set the memory, CPUs or SSH port of VM '$vm':" >&2 |
| 274 | | −删除: echo "$out" >&2 |
| 275 | | −删除: return 1 |
| 276 | | −删除: fi |
| 277 | | −删除: if [[ -n "$disk" ]] && ! out=$(cd /tmp && limactl edit "$vm" --disk "$disk" 2>&1); then |
| 278 | | −删除: echo "Warning: cannot set the disk of VM '$vm' to $disk GiB: it can grow, not shrink ('agent-vm setup --disk $disk' for a smaller base)." >&2 |
| 279 | | −删除: echo "$out" >&2 |
| 280 | | −删除: fi |
| 281 | | −删除: return 0 |
| 282 | | −删除:} |
| 283 | | −删除: |
| 284 | | −删除:# The env push and the write probe (see _agent_vm_push_env_and_probe), with |
| 285 | | −删除:# the caller's vm_name, host_dir, env_payload, guest_env, guest_runtime and |
| 286 | | −删除:# scratch, into its probe_out and is_writable: true, false, or lost when the |
| 287 | | −删除:# share is not mounted. |
| 288 | | −删除:_agent_vm_push_and_probe() { |
| 289 | | −删除: local st=0 |
| 290 | | −删除: probe_out="$(_agent_vm_push_env_and_probe "$vm_name" "$host_dir" "$env_payload" \ |
| 291 | | −删除: "$guest_env" "$guest_runtime")" || st=$? |
| 292 | | −删除: case "$st" in |
| 293 | | −删除: 0) is_writable="true" ;; |
| 294 | | −删除: 2) is_writable="lost" ;; |
| 295 | | −删除: *) is_writable="false" ;; |
| 296 | | −删除: esac |
| 297 | | −删除: [[ "$probe_out" == *env-ok* ]] || echo "Warning: failed to push the env files into VM '$vm_name'." >&2 |
| 298 | | −删除:} |
| 299 | | −删除: |
| 300 | | −删除:# Ensure the VM for cwd exists and is running, creating/starting as needed |
| 301 | | −删除:# Usage: _agent_vm_ensure_running <vm_name> <host_dir> [vm_opts...], the options |
| 302 | | −删除:# as _agent_vm_take_opt collects them (checked already). |
| 303 | | −删除:_agent_vm_ensure_running() { |
| 304 | | −删除: local vm_name="$1" |
| 305 | | −删除: local host_dir="$2" |
| 306 | | −删除: shift 2 |
| 307 | | −删除: local disk="" memory="" cpus="" ssh_port="" reset="" rdonly="" scratch="" _agent_vm_unsafe_git_flag="" _agent_vm_unsafe_no_prompts="" |
| 308 | | −删除: while [[ $# -gt 0 ]]; do |
| 309 | | −删除: case "$1" in |
| 310 | | −删除: --disk) disk="$2"; shift 2 ;; |
| 311 | | −删除: --memory) memory="$2"; shift 2 ;; |
| 312 | | −删除: --cpus) cpus="$2"; shift 2 ;; |
| 313 | | −删除: --ssh-port) ssh_port="$2"; shift 2 ;; |
| 314 | | −删除: --reset) reset=1; shift ;; |
| 315 | | −删除: --readonly) rdonly=1; shift ;; |
| 316 | | −删除: --scratch) scratch=1; shift ;; |
| 317 | | −删除: --unsafe-writable-git) _agent_vm_unsafe_git_flag=1; shift ;; |
| 318 | | −删除: --unsafe-disable-security-prompts) _agent_vm_unsafe_no_prompts=1; shift ;; |
| 319 | | −删除: *) shift ;; |
| 320 | | −删除: esac |
| 321 | | −删除: done |
| 322 | | −删除: |
| 323 | | −删除: # --scratch (see _agent_vm_run_scratch) shares nothing: no share check |
| 324 | | −删除: # below applies to it. |
| 325 | | −删除: if [[ -n "$scratch" && ( -n "$reset" || -n "$rdonly" ) ]]; then |
| 326 | | −删除: echo "Error: --scratch makes a new VM that shares nothing: --reset and --readonly do not go with it." >&2 |
| 327 | | −删除: return 1 |
| 328 | | −删除: fi |
| 329 | | −删除: |
| 330 | | −删除: local want_writable="true" |
| 331 | | −删除: [[ -n "$rdonly" ]] && want_writable="false" |
| 332 | | −删除: |
| 333 | | −删除: # Clamped once, so every path below compares and applies the same values. |
| 334 | | −删除: cpus="$(_agent_vm_cap_resource cpus "$cpus")" |
| 335 | | −删除: memory="$(_agent_vm_cap_resource memory "$memory")" |
| 336 | | −删除: _agent_vm_warn_disk_space "$disk" |
| 337 | | −删除: |
| 338 | | −删除: # Two VMs set to one port: the second would fail to start. |
| 339 | | −删除: if [[ -n "$ssh_port" && "$ssh_port" != 0 ]]; then |
| 340 | | −删除: local other |
| 341 | | −删除: other="$(limactl list --format '{{.Name}} {{.Config.SSH.LocalPort}}' 2>/dev/null \ |
| 342 | | −删除: | awk -v vm="$vm_name" -v p="$ssh_port" '$1 != vm && $2 == p { print $1; exit }')" |
| 343 | | −删除: if [[ -n "$other" ]]; then |
| 344 | | −删除: echo "Error: SSH port $ssh_port is already set for VM '$other'." >&2 |
| 345 | | −删除: return 1 |
| 346 | | −删除: fi |
| 347 | | −删除: fi |
| 348 | | −删除: |
| 349 | | −删除: # `cd ~ && agent-vm shell` would hand the VM your dotfiles and SSH keys, |
| 350 | | −删除: # read-write, and a share holding agent-vm's own files lets the VM change |
| 351 | | −删除: # what the host runs next. |
| 352 | | −删除: local unsafe |
| 353 | | −删除: if [[ -z "$scratch" ]] && unsafe="$(_agent_vm_unsafe_project "$host_dir")"; then |
| 354 | | −删除: echo "Error: refusing to share $host_dir with a VM: it $unsafe." >&2 |
| 355 | | −删除: echo "Run agent-vm from a project directory." >&2 |
| 356 | | −删除: return 1 |
| 357 | | −删除: fi |
| 358 | | −删除: |
| 359 | | −删除: local bad |
| 360 | | −删除: if [[ -z "$scratch" ]] && bad="$(_agent_vm_unmountable_path "$host_dir")"; then |
| 361 | | −删除: echo "Error: project path $bad:" >&2 |
| 362 | | −删除: echo " $host_dir" >&2 |
| 363 | | −删除: echo "Rename the directory, then retry." >&2 |
| 364 | | −删除: return 1 |
| 365 | | −删除: fi |
| 366 | | −删除: |
| 367 | | −删除: # A run interrupted while Lima created it leaves a folder without lima.yaml. |
| 368 | | −删除: _agent_vm_clean_partial_state "$vm_name" |
| 369 | | −删除: |
| 370 | | −删除: if ! _agent_vm_base_exists; then |
| 371 | | −删除: # Distinguish "never set up" from "setup died halfway": the second one |
| 372 | | −删除: # leaves a template that looks fine in `limactl list` but is empty. |
| 373 | | −删除: if _agent_vm_exists "$AGENT_VM_TEMPLATE"; then |
| 374 | | −删除: echo "Error: Base VM setup did not complete. Run 'agent-vm setup' again." >&2 |
| 375 | | −删除: else |
| 376 | | −删除: echo "Error: Base VM not found. Run 'agent-vm setup' first." >&2 |
| 377 | | −删除: fi |
| 378 | | −删除: return 1 |
| 379 | | −删除: fi |
| 380 | | −删除: |
| 381 | | −删除: # The security checks, before anything changes. A running VM is asked |
| 382 | | −删除: # nothing until stopped (see _agent_vm_recheck_after_stop). |
| 383 | | −删除: local vm_up="" protect_git="" ro_names="" hooks_notes="" |
| 384 | | −删除: [[ -z "$reset" ]] && _agent_vm_running "$vm_name" && vm_up=1 |
| 385 | | −删除: _agent_vm_start_checks "$host_dir" "$vm_name" || return 1 |
| 386 | | −删除: |
| 387 | | −删除: # Destroy existing VM if --reset was requested |
| 388 | | −删除: if [[ -n "$reset" ]] && _agent_vm_exists "$vm_name"; then |
| 389 | | −删除: echo "Resetting VM '$vm_name'..." |
| 390 | | −删除: _agent_vm_delete_vm "$vm_name" || return 1 |
| 391 | | −删除: fi |
| 392 | | −删除: |
| 393 | | −删除: local is_new_vm="" apply_resize="" |
| 394 | | −删除: local file_mount_entries=() |
| 395 | | −删除: local file_mounts_cache="$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}" |
| 396 | | −删除: |
| 397 | | −删除: if ! _agent_vm_exists "$vm_name"; then |
| 398 | | −删除: is_new_vm=1 |
| 399 | | −删除: echo "Creating VM '$vm_name'..." |
| 400 | | −删除: local create_out |
| 401 | | −删除: if ! create_out=$(limactl clone "$AGENT_VM_TEMPLATE" "$vm_name" --tty=false 2>&1); then |
| 402 | | −删除: echo "Error: could not clone the base template into '$vm_name':" >&2 |
| 403 | | −删除: echo "$create_out" >&2 |
| 404 | | −删除: return 1 |
| 405 | | −删除: fi |
| 406 | | −删除: # Which agent-vm built its base: none recorded for a base of 0.1.0, which |
| 407 | | −删除: # has no sshfs for the protected shares. Then migrated before the edit, |
| 408 | | −删除: # on the bare clone (see _agent_vm_migrate_0_1). |
| 409 | | −删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-built-by-${vm_name}" "$AGENT_VM_STATE_DIR/.agent-vm-sshfs-${vm_name}" |
| 410 | | −删除: if [[ -f "$AGENT_VM_STATE_DIR/.agent-vm-base-built-by" ]]; then |
| 411 | | −删除: cp "$AGENT_VM_STATE_DIR/.agent-vm-base-built-by" "$AGENT_VM_STATE_DIR/.agent-vm-built-by-${vm_name}" |
| 412 | | −删除: fi |
| 413 | | −删除: if [[ -n "$protect_git" ]] && ! _agent_vm_migrate_0_1_if_needed "$vm_name"; then |
| 414 | | −删除: _agent_vm_delete_vm "$vm_name" |
| 415 | | −删除: return 1 |
| 416 | | −删除: fi |
| 417 | | −删除: # Configured before its first start. A clone that could not be holds |
| 418 | | −删除: # nothing yet: it goes, and the next run starts over (#21). |
| 419 | | −删除: if ! _agent_vm_apply_shares "$vm_name" "$host_dir" "$want_writable" \ |
| 420 | | −删除: || ! _agent_vm_apply_resources "$vm_name"; then |
| 421 | | −删除: _agent_vm_delete_vm "$vm_name" |
| 422 | | −删除: return 1 |
| 423 | | −删除: fi |
| 424 | | −删除: printf '%s' "$hooks_notes" |
| 425 | | −删除: # Which base it was cloned from, for the stale warning below. |
| 426 | | −删除: local base_ver="$AGENT_VM_STATE_DIR/.agent-vm-base-version" |
| 427 | | −删除: if [[ -f "$base_ver" ]]; then |
| 428 | | −删除: cp "$base_ver" "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" |
| 429 | | −删除: fi |
| 430 | | −删除: elif { [[ -n "$disk" || -n "$memory" || -n "$cpus" ]] \ |
| 431 | | −删除: && _agent_vm_resources_differ "$vm_name" "$cpus" "$memory" "$disk"; } \ |
| 432 | | −删除: || _agent_vm_ssh_port_differs "$vm_name" "$ssh_port"; then |
| 433 | | −删除: # Only when the request differs from what the VM has: a caller passing |
| 434 | | −删除: # its defaults every time would otherwise be asked every time. Declined, |
| 435 | | −删除: # the VM keeps its settings and everything below still applies. |
| 436 | | −删除: apply_resize=1 |
| 437 | | −删除: if _agent_vm_running "$vm_name"; then |
| 438 | | −删除: echo "VM '$vm_name' is currently running. It must be stopped to apply new settings." |
| 439 | | −删除: if _agent_vm_can_ask && [[ "$(_agent_vm_ask_yn "Stop the VM and apply changes?" N)" == "1" ]]; then |
| 440 | | −删除: echo "Stopping VM..." |
| 441 | | −删除: _agent_vm_stop_to_change "$vm_name" "$host_dir" || return 1 |
| 442 | | −删除: else |
| 443 | | −删除: echo "Not applied: the VM keeps its current settings." |
| 444 | | −删除: apply_resize="" |
| 445 | | −删除: fi |
| 446 | | −删除: fi |
| 447 | | −删除: fi |
| 448 | | −删除: if [[ -n "$apply_resize" ]]; then |
| 449 | | −删除: echo "Updating VM settings..." |
| 450 | | −删除: _agent_vm_apply_resources "$vm_name" || return 1 |
| 451 | | −删除: fi |
| 452 | | −删除: |
| 453 | | −删除: # The same verdict as `info`'s vm_stale=. |
| 454 | | −删除: if [[ "$(_agent_vm_stale_state "$vm_name")" == "1" ]]; then |
| 455 | | −删除: echo "Warning: Base VM has been updated since this VM was cloned. Use --reset to re-clone from the new base." >&2 |
| 456 | | −删除: fi |
| 457 | | −删除: |
| 458 | | −删除: # Whether another session may be using it: read before the start below. |
| 459 | | −删除: local was_running="" |
| 460 | | −删除: _agent_vm_running "$vm_name" && was_running=1 |
| 461 | | −删除: |
| 462 | | −删除: # Shares recorded otherwise than asked: the .git protection (git_stale), its |
| 463 | | −删除: # names (names_stale), the mode (mode_stale), sshfs's cache (cache_stale). A |
| 464 | | −删除: # stopped VM is changed before it boots, so nothing starting with it gets a |
| 465 | | −删除: # writable window; a running one keeps its shares until stopped, or is |
| 466 | | −删除: # offered a restart (never for the cache alone). |
| 467 | | −删除: local was_protected="" git_stale="" names_stale="" mode_stale="" cache_stale="" |
| 468 | | −删除: _agent_vm_mounts_protect_git "$vm_name" && was_protected=1 |
| 469 | | −删除: if [[ -n "$protect_git" && -n "$was_protected" ]]; then |
| 470 | | −删除: # The record against what Lima has for an existing VM: a share without |
| 471 | | −删除: # names, set outside agent-vm, leaves it unprotected whatever was |
| 472 | | −删除: # recorded. Which names, the record says (names_stale below). |
| 473 | | −删除: if [[ -z "$is_new_vm" ]] \ |
| 474 | | −删除: && ! _agent_vm_live_shares_why "$(_agent_vm_live_config "$vm_name")" >/dev/null; then |
| 475 | | −删除: was_protected="" |
| 476 | | −删除: # Running, it is served by the limactl that started it, which may not be |
| 477 | | −删除: # the one checked above. |
| 478 | | −删除: elif [[ -n "$was_running" ]] && ! _agent_vm_hostagent_is_limactl "$vm_name"; then |
| 479 | | −删除: echo "Warning: VM '$vm_name' was started by another limactl than $(_agent_vm_limactl_path), which may not keep .git read-only." >&2 |
| 480 | | −删除: was_protected="" |
| 481 | | −删除: fi |
| 482 | | −删除: fi |
| 483 | | −删除: [[ "$was_protected" != "$protect_git" ]] && git_stale=1 |
| 484 | | −删除: if [[ -n "$protect_git" && -n "$was_protected" ]]; then |
| 485 | | −删除: _agent_vm_mounts_have_readonly_names "$vm_name" "$ro_names" || names_stale=1 |
| 486 | | −删除: # Shares made otherwise than _agent_vm_build_mounts_json would now (sshfs's |
| 487 | | −删除: # cache): set again before it boots, never a reason to restart it. |
| 488 | | −删除: _agent_vm_mounts_sshfs_current "$vm_name" || cache_stale=1 |
| 489 | | −删除: fi |
| 490 | | −删除: if [[ -n "$scratch" ]]; then |
| 491 | | −删除: # Shares nothing, as made: nothing to bring in line. |
| 492 | | −删除: git_stale="" |
| 493 | | −删除: elif [[ "$want_writable" == "false" ]]; then |
| 494 | | −删除: _agent_vm_mounts_all_readonly "$vm_name" || mode_stale=1 |
| 495 | | −删除: elif _agent_vm_mounts_all_readonly "$vm_name"; then |
| 496 | | −删除: mode_stale=1 |
| 497 | | −删除: fi |
| 498 | | −删除: # A running VM lacking a protection it should have: restarting it cuts its |
| 499 | | −删除: # other sessions, so it is asked; declined, it is a risk to accept. Not |
| 500 | | −删除: # under --readonly, which restarts it below anyway. |
| 501 | | −删除: if [[ -z "$rdonly" && -n "$was_running" && -n "$protect_git" && ( -n "$git_stale" || -n "$names_stale" ) ]]; then |
| 502 | | −删除: if [[ -n "$git_stale" ]]; then |
| 503 | | −删除: echo "Warning: VM '$vm_name' is running with .git writable, so the agent can still write .git." >&2 |
| 504 | | −删除: else |
| 505 | | −删除: echo "Warning: VM '$vm_name' runs with an older list of read-only names (it needs $(_agent_vm_names_text "$ro_names")), so the agent can still write the new ones." >&2 |
| 506 | | −删除: fi |
| 507 | | −删除: if ! _agent_vm_prompts_disabled_by >/dev/null && _agent_vm_can_ask \ |
| 508 | | −删除: && [[ "$(_agent_vm_ask_yn "Restart it now to apply them? Sessions using it are cut." Y)" == "1" ]]; then |
| 509 | | −删除: _agent_vm_stop_to_change "$vm_name" "$host_dir" || return 1 |
| 510 | | −删除: was_running="" |
| 511 | | −删除: elif ! _agent_vm_confirm_unsafe; then |
| 512 | | −删除: echo "Aborted. 'agent-vm stop', then run again." >&2 |
| 513 | | −删除: return 1 |
| 514 | | −删除: fi |
| 515 | | −删除: fi |
| 516 | | −删除: if [[ -n "$git_stale" && -n "$was_running" ]]; then |
| 517 | | −删除: if [[ -n "$protect_git" ]]; then |
| 518 | | −删除: : |
| 519 | | −删除: elif _agent_vm_writable_git_optout; then |
| 520 | | −删除: echo "Note: VM '$vm_name' keeps .git read-only until it stops." >&2 |
| 521 | | −删除: else |
| 522 | | −删除: echo "Warning: this Lima cannot keep .git read-only; VM '$vm_name' keeps its current shares until it stops." >&2 |
| 523 | | −删除: fi |
| 524 | | −删除: elif [[ -n "$names_stale" && -n "$was_running" ]]; then |
| 525 | | −删除: : |
| 526 | | −删除: elif [[ -z "$was_running" && ( -n "$git_stale" || -n "$names_stale" || -n "$mode_stale" || -n "$cache_stale" ) ]]; then |
| 527 | | −删除: if [[ -n "$git_stale" ]]; then |
| 528 | | −删除: if [[ -n "$protect_git" ]]; then |
| 529 | | −删除: echo "Making every .git read-only for VM '$vm_name'..." |
| 530 | | −删除: elif _agent_vm_writable_git_optout; then |
| 531 | | −删除: echo "Making .git writable for VM '$vm_name' ($(_agent_vm_writable_git_why))..." >&2 |
| 532 | | −删除: else |
| 533 | | −删除: echo "Warning: this Lima cannot keep .git read-only; VM '$vm_name' goes back to Lima's default mount type." >&2 |
| 534 | | −删除: fi |
| 535 | | −删除: elif [[ -n "$names_stale" ]]; then |
| 536 | | −删除: echo "Making $(_agent_vm_names_text "$ro_names") read-only for VM '$vm_name'..." |
| 537 | | −删除: fi |
| 538 | | −删除: [[ -n "$git_stale" || -n "$names_stale" ]] && printf '%s' "$hooks_notes" |
| 539 | | −删除: if [[ -n "$protect_git" ]]; then |
| 540 | | −删除: _agent_vm_migrate_0_1_if_needed "$vm_name" || return 1 |
| 541 | | −删除: fi |
| 542 | | −删除: if [[ -n "$mode_stale" && "$want_writable" == "false" ]]; then |
| 543 | | −删除: echo "Making every share of VM '$vm_name' read-only..." |
| 544 | | −删除: elif [[ -n "$mode_stale" ]]; then |
| 545 | | −删除: echo "VM '$vm_name' was left read-only by --readonly; making it writable again..." |
| 546 | | −删除: fi |
| 547 | | −删除: _agent_vm_apply_shares "$vm_name" "$host_dir" "$want_writable" || return 1 |
| 548 | | −删除: fi |
| 549 | | −删除: |
| 550 | | −删除: if [[ -z "$was_running" ]]; then |
| 551 | | −删除: _agent_vm_check_before_start "$vm_name" || return 1 |
| 552 | | −删除: echo "Starting VM '$vm_name'..." |
| 553 | | −删除: local start_log |
| 554 | | −删除: if ! start_log=$(limactl start "$vm_name" 2>&1); then |
| 555 | | −删除: local ha_log |
| 556 | | −删除: ha_log="$(_agent_vm_ha_log "$vm_name")" |
| 557 | | −删除: echo "Error: Failed to start VM '$vm_name'." >&2 |
| 558 | | −删除: echo "--- limactl start output ---" >&2 |
| 559 | | −删除: echo "$start_log" >&2 |
| 560 | | −删除: echo "Full log: $ha_log" >&2 |
| 561 | | −删除: _agent_vm_windows_start_hint "$ha_log" |
| 562 | | −删除: return 1 |
| 563 | | −删除: fi |
| 564 | | −删除: fi |
| 565 | | −删除: |
| 566 | | −删除: # Bring the shares of the running VM in line with the mode asked: a write |
| 567 | | −删除: # probe on the project (see _agent_vm_push_env_and_probe, which pushes the |
| 568 | | −删除: # env too), and the record for the other shares, a missing one counting as |
| 569 | | −删除: # writable. A mismatch is a stop, edit and start: a broken mount repaired, |
| 570 | | −删除: # --readonly applied, or a --readonly VM made writable again. |
| 571 | | −删除: # --scratch takes nothing of the project but ~/.agent-vm/env, and works at |
| 572 | | −删除: # the project's path on the VM's own disk. |
| 573 | | −删除: local is_writable="false" probe_out guest_env="" guest_runtime="" project_env="" project_runtime="" env_payload |
| 574 | | −删除: if [[ -n "$scratch" ]]; then |
| 575 | | −删除: if ! limactl shell "$vm_name" sh -c 'sudo install -d -o "$(id -u)" -g "$(id -g)" "$1"' sh "$host_dir" </dev/null >/dev/null 2>&1; then |
| 576 | | −删除: echo "Error: could not make $host_dir in VM '$vm_name'." >&2 |
| 577 | | −删除: return 1 |
| 578 | | −删除: fi |
| 579 | | −删除: env_payload="$( [ ! -f "$AGENT_VM_STATE_DIR/env" ] || _agent_vm_strip_cr < "$AGENT_VM_STATE_DIR/env" )" |
| 580 | | −删除: else |
| 581 | | −删除: project_env="$(_agent_vm_project_env_file "$host_dir")" |
| 582 | | −删除: project_runtime="$(_agent_vm_project_runtime_path "$host_dir")" |
| 583 | | −删除: # By their path in the project, the one the VM has. |
| 584 | | −删除: local in_rel |
| 585 | | −删除: in_rel="$(_agent_vm_project_rel "$host_dir" "$project_env")" && guest_env="$host_dir/$in_rel" |
| 586 | | −删除: in_rel="$(_agent_vm_project_rel "$host_dir" "$project_runtime")" && guest_runtime="$host_dir/$in_rel" |
| 587 | | −删除: env_payload="$(_agent_vm_env_payload "$host_dir")" |
| 588 | | −删除: fi |
| 589 | | −删除: _agent_vm_push_and_probe |
| 590 | | −删除: # A scratch VM has no share to repair: the repair below would mount the |
| 591 | | −删除: # project into it. |
| 592 | | −删除: if [[ -n "$scratch" && "$is_writable" != "true" ]]; then |
| 593 | | −删除: echo "Error: $host_dir is not writable in VM '$vm_name'." >&2 |
| 594 | | −删除: return 1 |
| 595 | | −删除: fi |
| 596 | | −删除: local needs_remount="" |
| 597 | | −删除: [[ "$is_writable" != "$want_writable" ]] && needs_remount=1 |
| 598 | | −删除: if [[ "$want_writable" == "false" ]] && ! _agent_vm_mounts_all_readonly "$vm_name"; then |
| 599 | | −删除: needs_remount=1 |
| 600 | | −删除: fi |
| 601 | | −删除: |
| 602 | | −删除: if [[ -n "$needs_remount" ]]; then |
| 603 | | −删除: # Stopping kills whatever else is using this VM, so ask first rather than |
| 604 | | −删除: # yanking a running session out from under another terminal, in both |
| 605 | | −删除: # directions. The repair direction does not ask: a broken mount is |
| 606 | | −删除: # already unusable. |
| 607 | | −删除: if [[ "$is_writable" == lost ]]; then |
| 608 | | −删除: echo "Warning: the project share is not mounted in VM '$vm_name': what was written in $host_dir in the VM since then is on the VM's own disk, and hidden once the share is back." >&2 |
| 609 | | −删除: echo "Project share is not mounted; repairing..." >&2 |
| 610 | | −删除: elif [[ "$want_writable" == "true" && -n "$was_running" ]] && _agent_vm_mounts_all_readonly "$vm_name"; then |
| 611 | | −删除: echo "VM '$vm_name' runs read-only (--readonly). It must be restarted to make its shares writable." |
| 612 | | −删除: if ! _agent_vm_can_ask || [[ "$(_agent_vm_ask_yn "Stop the VM and make it writable? Sessions using it are cut." N)" != "1" ]]; then |
| 613 | | −删除: echo "Error: not restarted. Pass --readonly to use it as it is, or 'agent-vm stop' it first." >&2 |
| 614 | | −删除: return 1 |
| 615 | | −删除: fi |
| 616 | | −删除: elif [[ "$want_writable" == "false" ]] && [[ -n "$was_running" ]]; then |
| 617 | | −删除: echo "VM '$vm_name' was already running. It must be restarted to make its shares read-only." |
| 618 | | −删除: if ! _agent_vm_can_ask || [[ "$(_agent_vm_ask_yn "Stop the VM and apply --readonly?" N)" != "1" ]]; then |
| 619 | | −删除: # Not "continue with current settings" like the resize path does: |
| 620 | | −删除: # carrying on writable after --readonly was asked for is the one |
| 621 | | −删除: # outcome that must not be silent. |
| 622 | | −删除: echo "Error: --readonly was requested but not applied. Aborting." >&2 |
| 623 | | −删除: return 1 |
| 624 | | −删除: fi |
| 625 | | −删除: elif [[ "$want_writable" == "true" ]]; then |
| 626 | | −删除: echo "Project mount is not writable; repairing..." >&2 |
| 627 | | −删除: fi |
| 628 | | −删除: |
| 629 | | −删除: # Stopped for sure first: recording read-only shares while it still runs |
| 630 | | −删除: # writable would claim what is not true. |
| 631 | | −删除: _agent_vm_stop_to_change "$vm_name" "$host_dir" || return 1 |
| 632 | | −删除: if [[ -n "$protect_git" ]]; then |
| 633 | | −删除: _agent_vm_migrate_0_1_if_needed "$vm_name" || return 1 |
| 634 | | −删除: fi |
| 635 | | −删除: _agent_vm_apply_shares "$vm_name" "$host_dir" "$want_writable" || return 1 |
| 636 | | −删除: _agent_vm_check_before_start "$vm_name" || return 1 |
| 637 | | −删除: # Checked: the probe below cannot tell a dead VM from a read-only one. |
| 638 | | −删除: if ! limactl start "$vm_name" &>/dev/null; then |
| 639 | | −删除: echo "Error: VM '$vm_name' did not come back up after changing the project mount." >&2 |
| 640 | | −删除: return 1 |
| 641 | | −删除: fi |
| 642 | | −删除: # Again: in the repair case the first probe could not see the project's |
| 643 | | −删除: # env file and runtime script. |
| 644 | | −删除: _agent_vm_push_and_probe |
| 645 | | −删除: if [[ "$is_writable" != "$want_writable" ]]; then |
| 646 | | −删除: if [[ "$is_writable" == lost ]]; then |
| 647 | | −删除: echo "Error: the project share did not mount in VM '$vm_name':" >&2 |
| 648 | | −删除: echo " $host_dir" >&2 |
| 649 | | −删除: echo "Lima's log: $(_agent_vm_ha_log "$vm_name")" >&2 |
| 650 | | −删除: elif [[ "$want_writable" == "true" ]]; then |
| 651 | | −删除: echo "Error: project directory is still not writable inside the VM:" >&2 |
| 652 | | −删除: echo " $host_dir" >&2 |
| 653 | | −删除: echo "The host mount failed to attach. Try 'agent-vm --reset <command>'" >&2 |
| 654 | | −删除: echo "to re-clone the VM from the base template." >&2 |
| 655 | | −删除: else |
| 656 | | −删除: echo "Error: failed to mount the project directory read-only:" >&2 |
| 657 | | −删除: echo " $host_dir" >&2 |
| 658 | | −删除: fi |
| 659 | | −删除: return 1 |
| 660 | | −删除: fi |
| 661 | | −删除: fi |
| 37 | +新增:dir="$(CDPATH= cd -P -- "$(dirname "$src")" >/dev/null && pwd)" |
| 662 | 38 | |
| 663 | | −删除: # --readonly is only claimed where the host enforces it (see |
| 664 | | −删除: # _agent_vm_mount_is_host_enforced); "could not tell" is refused too. |
| 665 | | −删除: if [[ -n "$rdonly" ]]; then |
| 666 | | −删除: _agent_vm_mount_is_host_enforced "$vm_name" |
| 667 | | −删除: case $? in |
| 668 | | −删除: 0) |
| 669 | | −删除: # Every share, those Lima's _config adds to every VM included. |
| 670 | | −删除: if [[ "$(_agent_vm_live_config "$vm_name")" == *'"writable":true'* ]]; then |
| 671 | | −删除: echo "Error: --readonly cannot hold: Lima gives VM '$vm_name' a writable share agent-vm did not set." >&2 |
| 672 | | −删除: echo " Look for 'mounts' in $(_agent_vm_lima_home)/_config/override.yaml and default.yaml." >&2 |
| 673 | | −删除: return 1 |
| 674 | | −删除: fi |
| 675 | | −删除: echo "Read-only: the project and every other share (enforced on the host)." ;; |
| 676 | | −删除: 1) echo "Error: --readonly cannot be enforced with this mount type." >&2 |
| 677 | | −删除: echo " Lima only applies it inside the guest for reverse-sshfs (without" >&2 |
| 678 | | −删除: echo " readonlyNames) and for virtiofs under QEMU, where root in the VM" >&2 |
| 679 | | −删除: echo " can remount it read-write. Use virtiofs on vz, or 9p on QEMU (both" >&2 |
| 680 | | −删除: echo " enforce it on the host), in your Lima config, then --reset." >&2 |
| 681 | | −删除: return 1 ;; |
| 682 | | −删除: *) echo "Error: could not determine the project mount type, so --readonly" >&2 |
| 683 | | −删除: echo " cannot be confirmed as enforced outside the VM. Aborting." >&2 |
| 684 | | −删除: return 1 ;; |
| 685 | | −删除: esac |
| 686 | | −删除: fi |
| 39 | +新增:# Windows (Git Bash): agent-vm.exe. |
| 40 | +新增:exe="" |
| 41 | +新增:case "$(uname -s)" in MINGW* | MSYS* | CYGWIN*) exe=.exe ;; esac |
| 687 | 42 | |
| 688 | | −删除: # The host's terminfo entry in the VM, for terminals Debian does not know |
| 689 | | −删除: # (xterm-ghostty, xterm-kitty): zsh cannot read their keys otherwise. Once |
| 690 | | −删除: # per VM and $TERM. |
| 691 | | −删除: local term_cache="$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}" |
| 692 | | −删除: if [[ -n "${TERM:-}" ]] && [[ "$(cat "$term_cache" 2>/dev/null)" != "$TERM" ]] \ |
| 693 | | −删除: && infocmp -x "$TERM" &>/dev/null; then |
| 694 | | −删除: if infocmp -x "$TERM" | limactl shell "$vm_name" sudo tic -x - &>/dev/null; then |
| 695 | | −删除: echo "$TERM" >| "$term_cache" |
| 696 | | −删除: else |
| 697 | | −删除: echo "Warning: failed to install '$TERM' terminfo inside VM." >&2 |
| 698 | | −删除: fi |
| 699 | | −删除: fi |
| 700 | | −删除: |
| 701 | | −删除: # Run per-user runtime script if it exists |
| 702 | | −删除: if [ -f "$AGENT_VM_STATE_DIR/runtime.sh" ]; then |
| 703 | | −删除: echo "Running user runtime setup..." |
| 704 | | −删除: _agent_vm_run_runtime "$vm_name" "$host_dir" "$AGENT_VM_STATE_DIR/runtime.sh" |
| 705 | | −删除: fi |
| 706 | | −删除: |
| 707 | | −删除: # Run project-specific runtime script if it exists: by its path in the VM |
| 708 | | −删除: # when it is in the project (the probe above said whether it is there), |
| 709 | | −删除: # piped from the host when AGENT_VM_PROJECT_RUNTIME puts it elsewhere. |
| 710 | | −删除: if [[ -n "$guest_runtime" ]]; then |
| 711 | | −删除: if [[ "$probe_out" == *runtime-found* ]]; then |
| 712 | | −删除: echo "Running project runtime setup..." |
| 713 | | −删除: _agent_vm_run_project_runtime "$vm_name" "$host_dir" "$guest_runtime" |
| 714 | | −删除: fi |
| 715 | | −删除: elif [ -f "$project_runtime" ]; then |
| 716 | | −删除: echo "Running project runtime setup..." |
| 717 | | −删除: _agent_vm_run_runtime "$vm_name" "$host_dir" "$project_runtime" |
| 718 | | −删除: fi |
| 719 | | −删除: |
| 720 | | −删除: # Single files of ~/.agent-vm/volumes (see _agent_vm_build_mounts_json, which |
| 721 | | −删除: # lists them in the cache): an existing VM's staged copy refreshed, so an |
| 722 | | −删除: # edit saved by rename on the host shows, then each bound at its place in |
| 723 | | −删除: # the VM, in one round trip. The bind does not survive a restart, and is |
| 724 | | −删除: # skipped when there. |
| 725 | | −删除: if [[ -f "$file_mounts_cache" ]]; then |
| 726 | | −删除: local host_src host_staging bind_src bind_dst |
| 727 | | −删除: while IFS='|' read -r host_src host_staging bind_src bind_dst; do |
| 728 | | −删除: [[ -n "$host_staging" ]] || continue |
| 729 | | −删除: if [[ -n "$is_new_vm" ]]; then |
| 730 | | −删除: : |
| 731 | | −删除: elif [[ ! -e "$host_src" ]]; then |
| 732 | | −删除: echo "Warning: Mount source '${host_src}' no longer exists; VM will see the last-staged copy." >&2 |
| 733 | | −删除: else |
| 734 | | −删除: _agent_vm_stage_file "$host_src" "$host_staging" \ |
| 735 | | −删除: || echo "Warning: Failed to refresh staged '${host_src}'; VM may see stale content." >&2 |
| 736 | | −删除: fi |
| 737 | | −删除: [[ -n "$bind_src" && -n "$bind_dst" ]] && file_mount_entries+=("${bind_src}|${bind_dst}") |
| 738 | | −删除: done < "$file_mounts_cache" |
| 739 | | −删除: if [[ ${#file_mount_entries[@]} -gt 0 ]]; then |
| 740 | | −删除: echo "Mounting individual files..." |
| 741 | | −删除: # The paths as arguments, never in the script. |
| 742 | | −删除: limactl shell "$vm_name" sudo bash -c ' |
| 743 | | −删除: set -e |
| 744 | | −删除: for entry in "$@"; do |
| 745 | | −删除: bind_src="${entry%%|*}" |
| 746 | | −删除: bind_dst="${entry#*|}" |
| 747 | | −删除: if ! findmnt -no TARGET "$bind_dst" >/dev/null 2>&1; then |
| 748 | | −删除: mkdir -p "$(dirname "$bind_dst")" && touch "$bind_dst" |
| 749 | | −删除: mount --bind "$bind_src" "$bind_dst" |
| 750 | | −删除: mount -o remount,ro,bind "$bind_dst" |
| 751 | | −删除: fi |
| 752 | | −删除: done |
| 753 | | −删除: ' -- "${file_mount_entries[@]}" |
| 754 | | −删除: fi |
| 755 | | −删除: fi |
| 756 | | −删除:} |
| 757 | | −删除: |
| 758 | | −删除:# --- agent-vm: the command, dispatching to the rest --------------------------- |
| 759 | | −删除: |
| 760 | | −删除:agent-vm() { |
| 761 | | −删除: # Options before the command are checked here and handed to it as written: |
| 762 | | −删除: # the commands that start a VM read them again, with their own. |
| 763 | | −删除: local lead=() vm_opts=() rm="" taken |
| 764 | | −删除: while [[ $# -gt 0 ]]; do |
| 765 | | −删除: _agent_vm_take_opt "$@" || return 1 |
| 766 | | −删除: [[ $taken -gt 0 ]] || break |
| 767 | | −删除: lead+=("${@:1:$taken}") |
| 768 | | −删除: shift "$taken" |
| 769 | | −删除: done |
| 770 | | −删除: |
| 771 | | −删除: local cmd="${1:-help}" |
| 772 | | −删除: shift 2>/dev/null || true |
| 773 | | −删除: |
| 774 | | −删除: # VM options mean nothing to the other commands: `agent-vm --readonly stop` |
| 775 | | −删除: # must not read as if something had been made read-only. |
| 776 | | −删除: if [[ ${#lead[@]} -gt 0 ]]; then |
| 777 | | −删除: case "$cmd" in |
| 778 | | −删除: stop|rm|destroy|destroy-all|list|status|name|info|env|project-env|version|--version|-V|doctor|install|uninstall|help|--help|-h) |
| 779 | | −删除: echo "Error: ${lead[*]:0:1} is an option for the commands that start a VM (claude, opencode, codex, vibe, pi, shell, run, code), not for '$cmd'." >&2 |
| 780 | | −删除: return 1 ;; |
| 781 | | −删除: esac |
| 782 | | −删除: fi |
| 783 | | −删除: |
| 784 | | −删除: # Every command except help/setup needs limactl present. (setup installs it |
| 785 | | −删除: # itself; help needs nothing.) Without this, stop/list/status/etc. would fail |
| 786 | | −删除: # with confusing empty output instead of a clear, actionable message. |
| 787 | | −删除: case "$cmd" in |
| 788 | | −删除: help|--help|-h|setup|version|--version|-V|name|info|env|project-env|doctor|install|uninstall) ;; |
| 789 | | −删除: *) |
| 790 | | −删除: if [[ -z "$(_agent_vm_limactl_path)" ]]; then |
| 791 | | −删除: echo "Error: limactl (Lima) not found. Run 'agent-vm setup' first, or install" >&2 |
| 792 | | −删除: echo "it from https://lima-vm.io/docs/installation/" >&2 |
| 793 | | −删除: return 1 |
| 794 | | −删除: fi ;; |
| 795 | | −删除: esac |
| 796 | | −删除: |
| 797 | | −删除: case "$cmd" in |
| 798 | | −删除: setup) |
| 799 | | −删除: _agent_vm_setup ${lead[@]+"${lead[@]}"} "$@" |
| 800 | | −删除: ;; |
| 801 | | −删除: claude|opencode|codex|vibe|pi) |
| 802 | | −删除: _agent_vm_agent "$cmd" ${lead[@]+"${lead[@]}"} "$@" |
| 803 | | −删除: ;; |
| 804 | | −删除: shell|sh) |
| 805 | | −删除: _agent_vm_shell ${lead[@]+"${lead[@]}"} "$@" |
| 806 | | −删除: ;; |
| 807 | | −删除: run) |
| 808 | | −删除: _agent_vm_run ${lead[@]+"${lead[@]}"} "$@" |
| 809 | | −删除: ;; |
| 810 | | −删除: code) |
| 811 | | −删除: _agent_vm_code ${lead[@]+"${lead[@]}"} "$@" |
| 812 | | −删除: ;; |
| 813 | | −删除: stop) |
| 814 | | −删除: _agent_vm_stop "$@" |
| 815 | | −删除: ;; |
| 816 | | −删除: rm|destroy) |
| 817 | | −删除: _agent_vm_destroy "$@" |
| 818 | | −删除: ;; |
| 819 | | −删除: destroy-all) |
| 820 | | −删除: _agent_vm_destroy_all "$@" |
| 821 | | −删除: ;; |
| 822 | | −删除: list|status) |
| 823 | | −删除: _agent_vm_list |
| 824 | | −删除: ;; |
| 825 | | −删除: name) |
| 826 | | −删除: local name_dir |
| 827 | | −删除: name_dir="$(_agent_vm_abs_dir "${1:-}")" || return 1 |
| 828 | | −删除: _agent_vm_name "$name_dir" |
| 829 | | −删除: ;; |
| 830 | | −删除: info) |
| 831 | | −删除: local info_dir |
| 832 | | −删除: info_dir="$(_agent_vm_abs_dir "${1:-}")" || return 1 |
| 833 | | −删除: _agent_vm_info "$info_dir" |
| 834 | | −删除: ;; |
| 835 | | −删除: env) |
| 836 | | −删除: _agent_vm_env env "$AGENT_VM_STATE_DIR/env" "" "$@" |
| 837 | | −删除: ;; |
| 838 | | −删除: project-env) |
| 839 | | −删除: local project_env_file project_top="" project_rel |
| 840 | | −删除: project_env_file="$(_agent_vm_project_env_file)" |
| 841 | | −删除: if project_rel="$(_agent_vm_project_rel "$(pwd)" "$project_env_file")"; then |
| 842 | | −删除: project_top="$(pwd)" |
| 843 | | −删除: project_env_file="$project_top/$project_rel" |
| 844 | | −删除: fi |
| 845 | | −删除: _agent_vm_env project-env "$project_env_file" "$project_top" "$@" || return $? |
| 846 | | −删除: # Only after a write, and only if it worked: that is when the file is |
| 847 | | −删除: # new to the repository and when the user is looking. |
| 848 | | −删除: if [ "${1:-}" = "set" ]; then |
| 849 | | −删除: _agent_vm_warn_unignored "$project_env_file" |
| 850 | | −删除: fi |
| 851 | | −删除: ;; |
| 852 | | −删除: version|--version|-V) |
| 853 | | −删除: _agent_vm_version "$@" |
| 854 | | −删除: ;; |
| 855 | | −删除: doctor) |
| 856 | | −删除: if [[ $# -gt 0 ]]; then |
| 857 | | −删除: echo "Usage: agent-vm doctor" >&2 |
| 858 | | −删除: return 2 |
| 859 | | −删除: fi |
| 860 | | −删除: _agent_vm_doctor |
| 861 | | −删除: ;; |
| 862 | | −删除: install) |
| 863 | | −删除: _agent_vm_install "$@" |
| 864 | | −删除: ;; |
| 865 | | −删除: uninstall) |
| 866 | | −删除: _agent_vm_uninstall "$@" |
| 867 | | −删除: ;; |
| 868 | | −删除: help|--help|-h) |
| 869 | | −删除: _agent_vm_help |
| 870 | | −删除: ;; |
| 871 | | −删除: *) |
| 872 | | −删除: echo "Unknown command: $cmd" >&2 |
| 873 | | −删除: echo "Run 'agent-vm help' for usage." >&2 |
| 874 | | −删除: return 1 |
| 875 | | −删除: ;; |
| 876 | | −删除: esac |
| 877 | | −删除:} |
| 878 | | −删除: |
| 879 | | −删除:# --- commands ----------------------------------------------------------------- |
| 880 | | −删除: |
| 881 | | −删除:# Run a command in the VM through a login zsh. |
| 882 | | −删除:# |
| 883 | | −删除:# `limactl shell VM cmd` runs cmd under the shell configured for the instance, |
| 884 | | −删除:# and Lima >= 2.2.0 defaults that to /bin/bash whatever the guest login shell |
| 885 | | −删除:# is (lima-vm/lima#5194); older versions used "$SHELL", i.e. the login shell |
| 886 | | −删除:# `agent-vm.setup.sh` sets with chsh. Agent PATH entries and the |
| 887 | | −删除:# ~/.agent-vm.env sourcing both live in ~/.zshenv, so under bash the agents are |
| 888 | | −删除:# not found and anything that is found starts without its API keys. Forcing |
| 889 | | −删除:# `zsh -l -c` here works on every Lima version. |
| 890 | | −删除:# |
| 891 | | −删除:# The command and its arguments are passed as positional parameters, so the |
| 892 | | −删除:# guest shell never re-parses them; `env` runs the command and keeps leading |
| 893 | | −删除:# VAR=value assignments working. `env --`: GNU env still reads assignments |
| 894 | | −删除:# after it, and a command starting with `-` (`run -i foo`) is not taken for |
| 895 | | −删除:# one of its options. A command that is not there is said so, with env's |
| 896 | | −删除:# status: an agent installed only as an editor extension has no command. A |
| 897 | | −删除:# path is env's to run: it says why it cannot (a script without +x, 126). |
| 898 | | −删除:# |
| 899 | | −删除:# Usage: _agent_vm_lima_run <vm_name> <host_dir> <tty:1|""> <command> [args...] |
| 900 | | −删除:_agent_vm_lima_run() { |
| 901 | | −删除: local vm_name="$1" host_dir="$2" want_tty="$3" |
| 902 | | −删除: shift 3 |
| 903 | | −删除: local shell_opts=(--workdir "$host_dir") |
| 904 | | −删除: [[ -n "$want_tty" ]] && shell_opts+=(--tty) |
| 905 | | −删除: limactl shell "${shell_opts[@]}" "$vm_name" -- zsh -l -c \ |
| 906 | | −删除: 'case "$1" in *=*|*/*) ;; *) command -v -- "$1" >/dev/null || { print -r -- "agent-vm: $1 is not installed in this VM." >&2; exit 127; } ;; esac; exec env -- "$@"' \ |
| 907 | | −删除: agent-vm "$@" |
| 908 | | −删除:} |
| 909 | | −删除: |
| 910 | | −删除:# Starts this directory's VM with the caller's vm_opts, runs <function> with |
| 911 | | −删除:# the VM name and the directory before its own arguments, then deletes the VM |
| 912 | | −删除:# when the caller's rm is set. Returns the function's status. With --scratch, |
| 913 | | −删除:# see _agent_vm_run_scratch. |
| 914 | | −删除:_agent_vm_in_vm() { |
| 915 | | −删除: local fn="$1" host_dir vm_name st=0 o |
| 916 | | −删除: shift |
| 917 | | −删除: host_dir="$(pwd)" |
| 918 | | −删除: for o in ${vm_opts[@]+"${vm_opts[@]}"}; do |
| 919 | | −删除: if [[ "$o" == --scratch ]]; then |
| 920 | | −删除: _agent_vm_run_scratch "$fn" "$host_dir" "$@" |
| 921 | | −删除: return |
| 922 | | −删除: fi |
| 923 | | −删除: done |
| 924 | | −删除: vm_name="$(_agent_vm_name "$host_dir")" || return 1 |
| 925 | | −删除: _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"} || return 1 |
| 926 | | −删除: _agent_vm_print_resources "$vm_name" |
| 927 | | −删除: "$fn" "$vm_name" "$host_dir" "$@" || st=$? |
| 928 | | −删除: _agent_vm_reset_term_modes |
| 929 | | −删除: if [[ -n "$rm" ]]; then |
| 930 | | −删除: echo "Removing VM..." |
| 931 | | −删除: _agent_vm_destroy |
| 932 | | −删除: fi |
| 933 | | −删除: return "$st" |
| 934 | | −删除:} |
| 935 | | −删除: |
| 936 | | −删除:# --scratch: a new VM of its own (see _agent_vm_scratch_name), sharing |
| 937 | | −删除:# nothing, deleted once <function> returns, whatever it returns, unless the |
| 938 | | −删除:# user keeps it a while to look inside (asked below). Ctrl-C |
| 939 | | −删除:# included: the trap lets the run go on to the deletion instead of ending |
| 940 | | −删除:# the shell with the interrupted command (bash ends a script whose child dies |
| 941 | | −删除:# of SIGINT, unless it traps it). A run that is killed outright leaves its VM |
| 942 | | −删除:# recorded with its pid, and the next --scratch run deletes it. |
| 943 | | −删除:_agent_vm_run_scratch() { |
| 944 | | −删除: local fn="$1" host_dir="$2" vm_name st=0 old_int="" leftover |
| 945 | | −删除: shift 2 |
| 946 | | −删除: while IFS= read -r leftover; do |
| 947 | | −删除: [[ -n "$leftover" ]] || continue |
| 948 | | −删除: echo "Deleting scratch VM '$leftover', left by a run that did not finish..." |
| 949 | | −删除: _agent_vm_delete_vm "$leftover" |
| 950 | | −删除: done <<< "$(_agent_vm_scratch_leftovers)" |
| 951 | | −删除: vm_name="$(_agent_vm_scratch_name "$host_dir")" || return 1 |
| 952 | | −删除: mkdir -p "$AGENT_VM_STATE_DIR" && printf '%s\n' "$$" > "$(_agent_vm_scratch_marker "$vm_name")" || return 1 |
| 953 | | −删除: # The caller's: the shell session says the VM goes on exit. |
| 954 | | −删除: rm=1 |
| 955 | | −删除: old_int="$(trap -p INT)" |
| 956 | | −删除: trap ':' INT |
| 957 | | −删除: if _agent_vm_ensure_running "$vm_name" "$host_dir" ${vm_opts[@]+"${vm_opts[@]}"}; then |
| 958 | | −删除: _agent_vm_print_resources "$vm_name" |
| 959 | | −删除: "$fn" "$vm_name" "$host_dir" "$@" || st=$? |
| 960 | | −删除: _agent_vm_reset_term_modes |
| 961 | | −删除: # Asked before the deletion, when it can be (yes by default): a no opens |
| 962 | | −删除: # a shell in the VM, to look at what the command left, and asks again on |
| 963 | | −删除: # its exit. Only an explicit no keeps it: no terminal, or a question cut |
| 964 | | −删除: # short, deletes. |
| 965 | | −删除: while _agent_vm_can_ask \ |
| 966 | | −删除: && [[ "$(_agent_vm_ask_yn "Delete scratch VM '$vm_name'?" Y)" == "0" ]]; do |
| 967 | | −删除: echo "A shell in scratch VM '$vm_name'. Type 'exit' to be asked again." |
| 968 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" zsh -l |
| 969 | | −删除: _agent_vm_reset_term_modes |
| 970 | | −删除: done |
| 971 | | −删除: else |
| 972 | | −删除: st=1 |
| 973 | | −删除: fi |
| 974 | | −删除: echo "Deleting scratch VM '$vm_name'..." |
| 975 | | −删除: _agent_vm_delete_vm "$vm_name" || st=1 |
| 976 | | −删除: trap - INT |
| 977 | | −删除: [[ -z "$old_int" ]] || eval "$old_int" |
| 978 | | −删除: return "$st" |
| 979 | | −删除:} |
| 980 | | −删除: |
| 981 | | −删除:# `agent-vm <agent> [options] [args]`: the agent, with the flag that lets it |
| 982 | | −删除:# work unattended (safe in the VM), and a TTY for the full-screen ones. |
| 983 | | −删除:_agent_vm_agent() { |
| 984 | | −删除: local agent="$1" cmd=() tty="" vm_opts=() rm="" args=() |
| 985 | | −删除: shift |
| 986 | | −删除: case "$agent" in |
| 987 | | −删除: # The VM also enforces bypass mode with managed settings (see |
| 988 | | −删除: # agent-vm.setup.sh): Claude Code drops the flag when it relaunches itself. |
| 989 | | −删除: claude) cmd=(claude --dangerously-skip-permissions) ;; |
| 990 | | −删除: # Approves every prompt that is not explicitly denied: OpenCode's shipped |
| 991 | | −删除: # "yolo" (a --yolo flag was proposed, never merged). |
| 992 | | −删除: opencode) cmd=(opencode --auto); tty=1 ;; |
| 993 | | −删除: codex) cmd=(codex --dangerously-bypass-approvals-and-sandbox) ;; |
| 994 | | −删除: vibe) cmd=(vibe --agent auto-approve); tty=1 ;; |
| 995 | | −删除: # No permission prompts: it runs every tool as asked. |
| 996 | | −删除: pi) cmd=(pi); tty=1 ;; |
| 997 | | −删除: *) echo "Error: unknown agent: $agent" >&2; return 1 ;; |
| 998 | | −删除: esac |
| 999 | | −删除: _agent_vm_split_args "$@" || return 1 |
| 1000 | | −删除: _agent_vm_in_vm _agent_vm_lima_run "$tty" "${cmd[@]}" ${args[@]+"${args[@]}"} |
| 1001 | | −删除:} |
| 1002 | | −删除: |
| 1003 | | −删除:# `agent-vm shell [-c "command"]`: a login zsh in the VM, so ~/.zshenv (mise, |
| 1004 | | −删除:# PATH, ~/.agent-vm.env) is loaded. Takes no command of its own, so a word it |
| 1005 | | −删除:# does not know is an error, not something skipped as if it had applied. |
| 1006 | | −删除:_agent_vm_shell() { |
| 1007 | | −删除: local vm_opts=() rm="" cmd_string="" taken |
| 1008 | | −删除: while [[ $# -gt 0 ]]; do |
| 1009 | | −删除: _agent_vm_take_opt "$@" || return 1 |
| 1010 | | −删除: if [[ $taken -gt 0 ]]; then |
| 1011 | | −删除: shift "$taken" |
| 1012 | | −删除: continue |
| 1013 | | −删除: fi |
| 1014 | | −删除: case "$1" in |
| 1015 | | −删除: -c|--command) |
| 1016 | | −删除: if [[ $# -lt 2 || -z "$2" ]]; then |
| 1017 | | −删除: echo "Error: -c/--command requires a command string." >&2 |
| 1018 | | −删除: return 1 |
| 1019 | | −删除: fi |
| 1020 | | −删除: cmd_string="$2"; shift 2 ;; |
| 1021 | | −删除: *) |
| 1022 | | −删除: echo "Error: unknown argument for shell: $1" >&2 |
| 1023 | | −删除: echo "Usage: agent-vm [vm options] shell [-c \"command\"]" >&2 |
| 1024 | | −删除: return 1 ;; |
| 1025 | | −删除: esac |
| 1026 | | −删除: done |
| 1027 | | −删除: _agent_vm_in_vm _agent_vm_shell_session "$cmd_string" |
| 1028 | | −删除:} |
| 1029 | | −删除: |
| 1030 | | −删除:_agent_vm_shell_session() { |
| 1031 | | −删除: local vm_name="$1" host_dir="$2" cmd_string="$3" |
| 1032 | | −删除: if [[ -n "$cmd_string" ]]; then |
| 1033 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" zsh -l -c "$cmd_string" |
| 1034 | | −删除: return |
| 1035 | | −删除: fi |
| 1036 | | −删除: echo "VM: $vm_name | Dir: $host_dir" |
| 1037 | | −删除: if [[ -n "$rm" ]]; then |
| 1038 | | −删除: echo "Type 'exit' to leave. VM will be destroyed after exit." |
| 1039 | | −删除: else |
| 1040 | | −删除: echo "Type 'exit' to leave (VM keeps running). Use 'agent-vm stop' to stop it." |
| 1041 | | −删除: fi |
| 1042 | | −删除: limactl shell --workdir "$host_dir" "$vm_name" zsh -l |
| 1043 | | −删除:} |
| 1044 | | −删除: |
| 1045 | | −删除:# `agent-vm run [--tty] <command> [args]`. --tty gives the command a |
| 1046 | | −删除:# pseudo-terminal, which full-screen TUIs (htop, …) need to draw. |
| 1047 | | −删除:_agent_vm_run() { |
| 1048 | | −删除: local vm_opts=() rm="" tty="" args=() |
| 1049 | | −删除: _agent_vm_split_args "$@" || return 1 |
| 1050 | | −删除: if [[ ${#args[@]} -eq 0 ]]; then |
| 1051 | | −删除: echo "Usage: agent-vm run [--tty] <command> [args]" >&2 |
| 1052 | | −删除: return 1 |
| 1053 | | −删除: fi |
| 1054 | | −删除: _agent_vm_in_vm _agent_vm_lima_run "$tty" "${args[@]}" |
| 1055 | | −删除:} |
| 1056 | | −删除: |
| 1057 | | −删除:# Resolve the VM that `stop` / `rm` acts on, and prove it exists. |
| 1058 | | −删除:# Prints the name on stdout; diagnostics go to stderr. |
| 1059 | | −删除:# |
| 1060 | | −删除:# With no argument: the current directory's VM, as before. |
| 1061 | | −删除:# |
| 1062 | | −删除:# With one argument: a VM name as printed by `agent-vm list`. It has to be a |
| 1063 | | −删除:# NAME and not a directory, because the case it exists for is a VM no directory |
| 1064 | | −删除:# can reach any more: the name embeds a hash of the path, so renaming or |
| 1065 | | −删除:# deleting the project folder orphans the VM, and `_agent_vm_abs_dir` would |
| 1066 | | −删除:# refuse the old path anyway. `list` remains the only handle on it. |
| 1067 | | −删除:# |
| 1068 | | −删除:# The `agent-vm-` prefix is required: without it a typo could stop or delete an |
| 1069 | | −删除:# unrelated Lima instance on the same machine. |
| 1070 | | −删除:_agent_vm_resolve_target() { |
| 1071 | | −删除: local verb="$1" |
| 1072 | | −删除: shift |
| 1073 | | −删除: |
| 1074 | | −删除: local vm_name |
| 1075 | | −删除: case $# in |
| 1076 | | −删除: 0) vm_name="$(_agent_vm_name "$(pwd)")" || return 1 ;; |
| 1077 | | −删除: 1) |
| 1078 | | −删除: vm_name="$1" |
| 1079 | | −删除: case "$vm_name" in |
| 1080 | | −删除: agent-vm-*) ;; |
| 1081 | | −删除: *) |
| 1082 | | −删除: echo "Error: '$vm_name' is not an agent-vm VM name (they start with 'agent-vm-')." >&2 |
| 1083 | | −删除: echo "Run 'agent-vm list' to see them." >&2 |
| 1084 | | −删除: return 1 ;; |
| 1085 | | −删除: esac ;; |
| 1086 | | −删除: *) |
| 1087 | | −删除: echo "Usage: agent-vm $verb [vm-name]" >&2 |
| 1088 | | −删除: return 1 ;; |
| 1089 | | −删除: esac |
| 1090 | | −删除: |
| 1091 | | −删除: local st=0 |
| 1092 | | −删除: _agent_vm_exists "$vm_name" || st=$? |
| 1093 | | −删除: case "$st" in |
| 1094 | | −删除: 0) ;; |
| 1095 | | −删除: 1) |
| 1096 | | −删除: if [[ $# -eq 0 ]]; then |
| 1097 | | −删除: echo "No VM found for this directory." >&2 |
| 1098 | | −删除: echo "Run 'agent-vm list' to see existing VMs, then 'agent-vm $verb <vm-name>'." >&2 |
| 1099 | | −删除: else |
| 1100 | | −删除: echo "Error: no such VM: $vm_name" >&2 |
| 1101 | | −删除: echo "Run 'agent-vm list' to see existing VMs." >&2 |
| 1102 | | −删除: fi |
| 1103 | | −删除: return 1 ;; |
| 1104 | | −删除: *) |
| 1105 | | −删除: echo "Error: could not query Lima. Is it installed and working?" >&2 |
| 1106 | | −删除: return 1 ;; |
| 1107 | | −删除: esac |
| 1108 | | −删除: |
| 1109 | | −删除: echo "$vm_name" |
| 1110 | | −删除:} |
| 1111 | | −删除: |
| 1112 | | −删除:_agent_vm_stop() { |
| 1113 | | −删除: local vm_name |
| 1114 | | −删除: vm_name="$(_agent_vm_resolve_target stop "$@")" || return 1 |
| 1115 | | −删除: |
| 1116 | | −删除: echo "Stopping VM '$vm_name'..." |
| 1117 | | −删除: if ! _agent_vm_stop_vm "$vm_name"; then |
| 1118 | | −删除: echo "Error: VM '$vm_name' is still running, or Lima cannot say. See 'limactl list'." >&2 |
| 1119 | | −删除: return 1 |
| 1120 | | −删除: fi |
| 1121 | | −删除: echo "VM stopped." |
| 1122 | | −删除:} |
| 1123 | | −删除: |
| 1124 | | −删除:_agent_vm_destroy() { |
| 1125 | | −删除: local vm_name |
| 1126 | | −删除: vm_name="$(_agent_vm_resolve_target rm "$@")" || return 1 |
| 1127 | | −删除: |
| 1128 | | −删除: echo "Stopping and deleting VM '$vm_name'..." |
| 1129 | | −删除: _agent_vm_delete_vm "$vm_name" || return 1 |
| 1130 | | −删除: echo "VM destroyed." |
| 1131 | | −删除:} |
| 1132 | | −删除: |
| 1133 | | −删除:# Every agent-vm VM, the base template included: this is the command that |
| 1134 | | −删除:# gives the disk space back. `agent-vm setup` rebuilds the template. |
| 1135 | | −删除:_agent_vm_destroy_all() { |
| 1136 | | −删除: local list vms |
| 1137 | | −删除: if ! list="$(limactl list -q 2>/dev/null)"; then |
| 1138 | | −删除: echo "Error: could not query Lima. Is it installed and working?" >&2 |
| 1139 | | −删除: return 1 |
| 1140 | | −删除: fi |
| 1141 | | −删除: vms="$(printf '%s\n' "$list" | grep "^agent-vm-" || true)" |
| 1142 | | −删除: if [[ -z "$vms" ]]; then |
| 1143 | | −删除: echo "No agent-vm VMs found." |
| 1144 | | −删除: return 0 |
| 1145 | | −删除: fi |
| 1146 | | −删除: echo "This will destroy the following VMs:" |
| 1147 | | −删除: echo "$vms" |
| 1148 | | −删除: if _agent_vm_has_line "$vms" "$AGENT_VM_TEMPLATE"; then |
| 1149 | | −删除: echo "($AGENT_VM_TEMPLATE is the base template: 'agent-vm setup' rebuilds it.)" |
| 1150 | | −删除: fi |
| 1151 | | −删除: if ! _agent_vm_can_ask || [[ "$(_agent_vm_ask_yn "Continue?" N)" != "1" ]]; then |
| 1152 | | −删除: echo "Aborted." |
| 1153 | | −删除: return 0 |
| 1154 | | −删除: fi |
| 1155 | | −删除: _agent_vm_destroy_vms "$vms" || return 1 |
| 1156 | | −删除: echo "All VMs destroyed." |
| 1157 | | −删除:} |
| 1158 | | −删除: |
| 1159 | | −删除:# Stop, delete and forget each VM named on its own line in $1. Goes through |
| 1160 | | −删除:# all of them, and fails if one could not be deleted. |
| 1161 | | −删除:# limactl gets /dev/null as stdin (see _agent_vm_delete_vm): the names are |
| 1162 | | −删除:# read from stdin, and a limactl that reads it would swallow the ones still to |
| 1163 | | −删除:# come. |
| 1164 | | −删除:_agent_vm_destroy_vms() { |
| 1165 | | −删除: local vm st=0 |
| 1166 | | −删除: while IFS= read -r vm; do |
| 1167 | | −删除: [[ -n "$vm" ]] || continue |
| 1168 | | −删除: echo "Destroying $vm..." |
| 1169 | | −删除: _agent_vm_delete_vm "$vm" || st=1 |
| 1170 | | −删除: done <<< "$1" |
| 1171 | | −删除: return "$st" |
| 1172 | | −删除:} |
| 1173 | | −删除: |
| 1174 | | −删除:# Lima's table of the agent-vm VMs, with the base each was cloned from (see |
| 1175 | | −删除:# _agent_vm_base_label) in a last column. Lima pads every column but its last, |
| 1176 | | −删除:# so the lines are padded here to line it up. |
| 1177 | | −删除:# `list` and `status`: Lima's table of the agent-vm VMs, the current |
| 1178 | | −删除:# directory's marked with >, and the base each was cloned from (see |
| 1179 | | −删除:# _agent_vm_base_label) in a last column. Lima pads every column but its last, |
| 1180 | | −删除:# so the lines are padded here to line it up. |
| 1181 | | −删除:_agent_vm_list() { |
| 1182 | | −删除: local list line lines=() w=0 header=1 current mark |
| 1183 | | −删除: current="$(_agent_vm_name "$(pwd)" 2>/dev/null)" || current="" |
| 1184 | | −删除: if ! list="$(limactl list 2>/dev/null)"; then |
| 1185 | | −删除: echo "Error: could not query Lima. Is it installed and working?" >&2 |
| 1186 | | −删除: return 1 |
| 1187 | | −删除: fi |
| 1188 | | −删除: while IFS= read -r line; do |
| 1189 | | −删除: [[ -n "$line" ]] || continue |
| 1190 | | −删除: if [[ ${#lines[@]} -eq 0 || "$line" == agent-vm-* ]]; then |
| 1191 | | −删除: lines+=("$line") |
| 1192 | | −删除: [[ ${#line} -gt $w ]] && w=${#line} |
| 1193 | | −删除: fi |
| 1194 | | −删除: done <<< "$list" |
| 1195 | | −删除: if [[ ${#lines[@]} -lt 2 ]]; then |
| 1196 | | −删除: [[ ${#lines[@]} -eq 0 ]] || printf ' %s\n' "${lines[@]}" |
| 1197 | | −删除: echo " (no VMs)" |
| 1198 | | −删除: return 0 |
| 1199 | | −删除: fi |
| 1200 | | −删除: for line in "${lines[@]}"; do |
| 1201 | | −删除: if [[ -n "$header" ]]; then |
| 1202 | | −删除: header="" |
| 1203 | | −删除: printf ' %-*s %s\n' "$w" "$line" "BASE" |
| 1204 | | −删除: else |
| 1205 | | −删除: mark=" " |
| 1206 | | −删除: [[ "${line%% *}" == "$current" ]] && mark=">" |
| 1207 | | −删除: printf '%s %-*s %s\n' "$mark" "$w" "$line" "$(_agent_vm_base_label "${line%% *}")" |
| 1208 | | −删除: fi |
| 1209 | | −删除: done |
| 1210 | | −删除:} |
| 43 | +新增:if [ -f "$dir/agent-vm$exe" ]; then |
| 44 | +新增: exec "$dir/agent-vm$exe" "$@" |
| 45 | +新增:fi |
| 1211 | 46 | |
| 1212 | | −删除:# When the file is executed (the command `install` puts on PATH, or |
| 1213 | | −删除:# `./agent-vm.sh setup`) rather than sourced, dispatch to the agent-vm |
| 1214 | | −删除:# function so it doesn't silently no-op. BASH_SOURCE[0] differs from $0 when |
| 1215 | | −删除:# sourced. |
| 1216 | | −删除:if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then |
| 1217 | | −删除: agent-vm "$@" |
| 1218 | | −删除: exit $? |
| 47 | +新增:bin="$dir/_output/bin/agent-vm$exe" |
| 48 | +新增:head="$(git -C "$dir" rev-parse HEAD 2>/dev/null || echo none)" |
| 49 | +新增:if [ ! -x "$bin" ] || [ "$(cat "$dir/_output/built-from" 2>/dev/null)" != "$head" ]; then |
| 50 | +新增: echo "agent-vm: building (Go needed, and on macOS the Xcode command line tools; a few minutes the first time)..." >&2 |
| 51 | +新增: mkdir -p "$dir/_output" |
| 52 | +新增: make -C "$dir" agent-vm >"$dir/_output/build.log" 2>&1 \ |
| 53 | +新增: || { tail -n 20 "$dir/_output/build.log" >&2; echo "agent-vm: the build failed, full log: $dir/_output/build.log" >&2; exit 1; } |
| 54 | +新增: echo "$head" > "$dir/_output/built-from" |
| 1219 | 55 | fi |
| 56 | +新增:exec "$bin" "$@" |