跳到主要内容

Claude Cage · 面向 Claude 的隔离开发环境,支持桌面工作台、多账号、远程部署与移动端连接。上游 Lee-xeo/Claude-Cage 完整源码镜像,非灵栈原创;主许可 MIT,第三方组件遵循各自许可。来源:https://github.com/Lee-xeo/Claude-Cage 。手动同步,不附带 Release 安装包。

提交记录
修改README.md+74 −16看文件
@@ -27,6 +27,8 @@ echo "source $(pwd)/agent-vm.sh" >> ~/.zshrc # zsh
2727echo "source $(pwd)/agent-vm.sh" >> ~/.bashrc # or bash
2828```
2929
30新增:Sourcing puts `agent-vm` in your shell as a function. You can also invoke the script directly (`./agent-vm.sh setup`) without sourcing — useful for one-off runs.
31新增:
3032## Usage
3133
3234### One-time setup
@@ -35,18 +37,25 @@ echo "source $(pwd)/agent-vm.sh" >> ~/.bashrc # or bash
3537agent-vm setup
3638```
3739
38删除:Creates a base VM template with dev tools, Docker, Chromium, and AI coding agents pre-installed.
40新增:Creates a base VM template with dev tools, Docker, Chromium, and AI coding agents pre-installed. Run interactively to open the wizard; its first prompt offers a one-tap "default install" (everything except the opt-in languages Ruby, Rust, Go), answer `n` for per-component prompts. Pass `--preinstall=...` to skip the wizard. When stdin is not a terminal (CI), the wizard is auto-skipped and the default set is installed.
3941
4042Options:
4143
4244| Flag | Description | Default |
4345|------|-------------|---------|
4446| `--disk GB` | VM disk size in GB | 10 |
45删除:| `--memory GB` | VM memory in GB | 2 |
47新增:| `--memory GB` | VM memory in GB | 3 |
4648| `--cpus N` | Number of CPUs | 1 |
49新增:| `--preinstall=LIST` | Preinstall only this comma-separated subset in the base image (skips the wizard) | — |
50新增:
51新增:Names are lowercase: `python`, `node`, `ruby`, `rust`, `golang`, `docker`, `chromium`, `gh`, `claude`, `opencode`, `codex`, `vibe`. Use `default` for the default set (everything except Ruby/Rust/Go), `all` for everything, or `none` for nothing. Selecting `codex`, or `chromium` with any AI agent, also installs `node` because those paths require `npm`/`npx`.
4752
4853```bash
49删除:agent-vm setup --disk 50 --memory 16 --cpus 8 # Larger VM for heavy workloads
54新增:agent-vm setup # Interactive wizard
55新增:agent-vm setup --preinstall=default # Default set, no prompts
56新增:agent-vm setup --preinstall=default,rust # Default set plus Rust
57新增:agent-vm setup --preinstall=python,docker,claude # Minimal Claude-only setup
58新增:agent-vm setup --disk 50 --memory 16 --cpus 8 # Larger VM for heavy workloads
5059```
5160
5261### Run an agent in a VM
@@ -132,6 +141,49 @@ agent-vm --offline --readonly claude # Both
132141
133142## Customization
134143
144新增:### Sharing tokens across VMs: `~/.agent-vm/env`
145新增:
146新增:Put environment variables (API tokens, secrets, etc.) in this file as plain `KEY=value` lines — no `export` prefix, `#` for comments. They're auto-loaded into every shell in every VM:
147新增:
148新增:```bash
149新增:# ~/.agent-vm/env
150新增:GH_TOKEN=github_pat_xxxxxxxxxxxxxxxxxxxxxxxx
151新增:ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxxxxxx
152新增:OPENAI_API_KEY=sk-xxxxxxxxxxxxxxxxxxxxxxxx
153新增:MISTRAL_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxx
154新增:```
155新增:
156新增:These are picked up automatically by the tools that look for them: `gh` reads `GH_TOKEN`, Claude Code uses `ANTHROPIC_API_KEY` when not signed in, Codex uses `OPENAI_API_KEY`, Vibe uses `MISTRAL_API_KEY`, etc. The file is pushed into the VM at `~/.agent-vm.env` (mode 0600) on every `agent-vm` invocation, so edits propagate without `--reset`.
157新增:
158新增:For subscription-based auth (where you've already run `claude login` / `gh auth login` on the host), share the host's credentials directory via [`~/.agent-vm/volumes`](#extra-host-mounts-agent-vmvolumes) instead.
159新增:
160新增:### Extra host mounts: `~/.agent-vm/volumes`
161新增:
162新增:List host files or directories to mount inside every VM. One path per line, `~` is expanded, `#` starts a comment. Uses Docker Compose-style `source[:destination][:mode]` syntax, where `mode` is `ro` (default) or `rw`:
163新增:
164新增:```bash
165新增:# ~/.agent-vm/volumes
166新增:
167新增:# Mount at the same path in the VM (read-only)
168新增:~/.gitconfig
169新增:~/.gitignore
170新增:
171新增:# Mount at a different path in the VM (read-only)
172新增:# Note: the destination is used verbatim (no shell expansion) — replace
173新增:# $USER with your actual username. Only the source (left) side expands a
174新增:# leading ~.
175新增:~/.claude:/home/youruser.linux/.claude
176新增:
177新增:# Writable directory
178新增:~/.cache/shared:/home/youruser.linux/.cache/shared:rw
179新增:```
180新增:
181新增:When no destination is specified, the path is mounted at the same location inside the VM. Non-existent paths are skipped with a warning. Changes to this file take effect on new VMs (use `--reset` to re-apply to existing ones).
182新增:
183新增:`rw` is only supported for **directories**. Files are always read-only: with the hardlink/staging strategy used below, writable file mounts would silently desync on cross-filesystem setups. If you need a writable single file, mount its parent directory as `rw` instead. A destination literally named `ro` or `rw` is treated as a mode keyword — append an explicit `:ro`/`:rw` to disambiguate.
184新增:
185新增:Individual files are supported without exposing their parent directory: agent-vm hardlinks the source into a per-VM staging dir under `~/.agent-vm/file-mounts/<vm>/`, then bind-mounts it at the final destination on each VM start. If the source sits on a different filesystem (hardlink impossible), it falls back to a copy and live host changes won't propagate until the next VM restart. The staged hardlink is refreshed on each `agent-vm` invocation, so atomic-rename edits (common in editors) are picked up at the next VM (re)start.
186新增:
135187### Per-user setup: `~/.agent-vm/setup.sh`
136188
137189Create this file to install extra tools into the base VM template. It runs once during `agent-vm setup`, as the default VM user (with sudo available):
@@ -241,19 +293,25 @@ Each VM is fully isolated — agents must authenticate independently inside thei
241293| `agent-vm.sh` | Main script — source this in your shell config |
242294| `agent-vm.setup.sh` | Package installation script that runs inside the base VM during setup |
243295
244删除:## What's in the VM by default
245删除:
246删除:| Category | Packages |
247删除:|----------|----------|
248删除:| Core | git, curl, wget, jq, build-essential, unzip, zip |
249删除:| Python | python3, pip, venv |
250删除:| Version manager | [mise](https://mise.jdx.dev/) (Ruby, Python, Node, etc.) |
251删除:| Node.js | Node.js 24 LTS (via NodeSource) |
252删除:| Search | ripgrep, fd-find |
253删除:| Utilities | htop, GitHub CLI (gh) |
254删除:| Browser | Chromium (headless), xvfb |
255删除:| Containers | Docker Engine, Docker Compose |
256删除:| AI | Claude Code, OpenCode, Codex CLI, Chrome DevTools MCP server |
296新增:## What's in the VM
297新增:
298新增:The wizard's "default install" and `--preinstall=default` produce the same set: everything in the table below except the opt-in languages (Ruby, Rust, Go). Pass a different `--preinstall=` to install a different subset.
299新增:
300新增:| Category | Packages | Name | Installed by default? |
301新增:|----------|----------|------|----------------------|
302新增:| Core | git, curl, wget, jq, zsh, ca-certificates, build-essential, unzip, zip, ripgrep, fd-find, htop, iptables | (always) | always |
303新增:| Build libs | libssl-dev, libreadline-dev, zlib1g-dev, libyaml-dev, libffi-dev | (always) | always |
304新增:| Version manager | [mise](https://mise.jdx.dev/) | (always) | always |
305新增:| Python | python3, pip, venv | `python` | yes |
306新增:| Node.js | Node.js 24 LTS (via NodeSource) | `node` | yes |
307新增:| Ruby | ruby-full | `ruby` | no |
308新增:| Rust | rustup (stable toolchain) | `rust` | no |
309新增:| Go | golang-go | `golang` | no |
310新增:| GitHub CLI | gh | `gh` | yes |
311新增:| Browser | Chromium (headless), xvfb | `chromium` | yes |
312新增:| Containers | Docker Engine, Docker Compose | `docker` | yes |
313新增:| AI agents | Claude Code, OpenCode, Codex CLI, Mistral Vibe | `claude`, `opencode`, `codex`, `vibe` | yes |
314新增:| MCP | Chrome DevTools MCP (Claude/OpenCode/Codex/Vibe) | — | auto, when Node.js + Chromium + agent installed |
257315
258316## Security model
259317
修改agent-vm.setup.sh+193 −89看文件
@@ -10,99 +10,179 @@ set -euo pipefail
1010
1111export DEBIAN_FRONTEND=noninteractive
1212
13新增:# Component toggles. The host wizard prepends `export` lines for these before
14新增:# piping the script in. Members of the default install set (everything except
15新增:# Ruby/Rust/Go) default to 1 so running this script standalone (without the
16新增:# wizard) produces the same install you'd get from `agent-vm setup --preinstall=default`.
17新增:INSTALL_PYTHON="${AGENT_VM_INSTALL_PYTHON:-1}"
18新增:INSTALL_NODE="${AGENT_VM_INSTALL_NODE:-1}"
19新增:INSTALL_RUBY="${AGENT_VM_INSTALL_RUBY:-0}"
20新增:INSTALL_RUST="${AGENT_VM_INSTALL_RUST:-0}"
21新增:INSTALL_GOLANG="${AGENT_VM_INSTALL_GOLANG:-0}"
22新增:INSTALL_DOCKER="${AGENT_VM_INSTALL_DOCKER:-1}"
23新增:INSTALL_CHROMIUM="${AGENT_VM_INSTALL_CHROMIUM:-1}"
24新增:INSTALL_GH="${AGENT_VM_INSTALL_GH:-1}"
25新增:INSTALL_CLAUDE="${AGENT_VM_INSTALL_CLAUDE:-1}"
26新增:INSTALL_OPENCODE="${AGENT_VM_INSTALL_OPENCODE:-1}"
27新增:INSTALL_CODEX="${AGENT_VM_INSTALL_CODEX:-1}"
28新增:INSTALL_VIBE="${AGENT_VM_INSTALL_VIBE:-1}"
29新增:
30新增:# Several installers (Claude Code, Vibe, …) check PATH at install time and
31新增:# print a "~/.local/bin is not in your PATH" warning otherwise. The persistent
32新增:# PATH lives in ~/.zshrc / ~/.zshenv (added below), so once the user opens a
33新增:# VM shell it's fine — but this bash script runs under a fresh session that
34新增:# doesn't see those edits yet. Export it here so installers stay quiet.
35新增:export PATH="$HOME/.local/bin:$PATH"
36新增:
1337# Disable needrestart's interactive prompts
1438sudo mkdir -p /etc/needrestart/conf.d
1539echo '$nrconf{restart} = '"'"'a'"'"';' | sudo tee /etc/needrestart/conf.d/no-prompt.conf > /dev/null
1640
41新增:# Base packages always installed: core CLI tools plus the dev libraries needed
42新增:# to compile Ruby/Python/Node versions via mise (kept here so that toggling a
43新增:# language off doesn't strip the libs the user may still want to build with).
1744echo "Installing base packages..."
1845sudo apt-get update
1946sudo apt-get install -y \
2047 git curl jq zsh \
2148 wget build-essential \
22删除: python3 python3-pip python3-venv \
2349 ripgrep fd-find htop \
2450 unzip zip \
2551 ca-certificates \
2652 iptables \
2753 libssl-dev libreadline-dev zlib1g-dev libyaml-dev libffi-dev
2854
55新增:if [[ "$INSTALL_PYTHON" == "1" ]]; then
56新增: echo "Installing Python 3..."
57新增: sudo apt-get install -y python3 python3-pip python3-venv
58新增:fi
59新增:
60新增:if [[ "$INSTALL_RUBY" == "1" ]]; then
61新增: echo "Installing Ruby..."
62新增: sudo apt-get install -y ruby-full
63新增:fi
64新增:
65新增:if [[ "$INSTALL_GOLANG" == "1" ]]; then
66新增: echo "Installing Go..."
67新增: sudo apt-get install -y golang-go
68新增:fi
69新增:
70新增:if [[ "$INSTALL_RUST" == "1" ]]; then
71新增: # Rustup is the canonical Rust installer. --no-modify-path keeps it from
72新增: # editing ~/.profile/~/.bashrc — we add ~/.cargo/bin to zsh's PATH below.
73新增: echo "Installing Rust..."
74新增: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --no-modify-path --default-toolchain stable
75新增: echo 'export PATH=$HOME/.cargo/bin:$PATH' >> ~/.zshrc
76新增: echo 'export PATH=$HOME/.cargo/bin:$PATH' >> ~/.zshenv
77新增:fi
78新增:
2979# Set zsh as default shell
3080sudo chsh -s /usr/bin/zsh "$(whoami)"
3181
32删除:# Install mise (polyglot version manager for Ruby, Python, Node, etc.)
82新增:# Always set the VM prompt and put ~/.local/bin on PATH (mise installs there;
83新增:# Vibe's installer puts `vibe`/`vibe-acp` there too).
84新增:echo 'export PS1="vm:%1~%% "' >> ~/.zshrc
85新增:echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.zshrc
86新增:echo 'export PATH=$HOME/.local/bin:$PATH' >> ~/.zshenv
87新增:
88新增:# Auto-source ~/.agent-vm.env if present. The host pushes ~/.agent-vm/env into
89新增:# this path on every `agent-vm` invocation (see _agent_vm_ensure_running), so
90新增:# tokens/API keys defined there propagate to every shell in the VM. `set -a`
91新增:# auto-exports each KEY=value line, so the file content stays a plain dotenv.
92新增:echo '[ -f "$HOME/.agent-vm.env" ] && { set -a; . "$HOME/.agent-vm.env"; set +a; }' >> ~/.zshenv
93新增:
94新增:# Install mise (polyglot version manager for Ruby, Python, Node, etc.).
95新增:# Always installed so users can `mise install ruby@latest`, etc., even when
96新增:# they've opted out of preinstalled Node.
3397echo "Installing mise..."
3498curl https://mise.run | sh
3599echo 'eval "$(~/.local/bin/mise activate zsh)"' >> ~/.zshrc
36100echo 'eval "$(~/.local/bin/mise activate zsh)"' >> ~/.zshenv
37101
38删除:# Install Docker from official repo (includes docker compose)
39删除:echo "Installing Docker..."
40删除:sudo install -m 0755 -d /etc/apt/keyrings
41删除:sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
42删除:sudo chmod a+r /etc/apt/keyrings/docker.asc
43删除:echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
44删除:sudo apt-get update
45删除:sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
46删除:sudo usermod -aG docker "$(whoami)"
47删除:
48删除:# Install Node.js 24 LTS (needed for MCP servers)
49删除:echo "Installing Node.js 24..."
50删除:curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash -
51删除:sudo apt-get install -y nodejs
52删除:
53删除:# Install Chromium and dependencies for headless browsing
54删除:echo "Installing Chromium..."
55删除:sudo apt-get install -y chromium fonts-liberation xvfb
56删除:sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome
57删除:sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome-stable
58删除:sudo mkdir -p /opt/google/chrome
59删除:sudo ln -sf /usr/bin/chromium /opt/google/chrome/chrome
60删除:
61删除:# Install GitHub CLI from official repo
62删除:echo "Installing GitHub CLI..."
63删除:sudo mkdir -p -m 755 /etc/apt/keyrings
64删除:wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null
65删除:sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
66删除:echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
67删除:sudo apt-get update
68删除:sudo apt-get install -y gh
102新增:if [[ "$INSTALL_DOCKER" == "1" ]]; then
103新增: # Install Docker from official repo (includes docker compose)
104新增: echo "Installing Docker..."
105新增: sudo install -m 0755 -d /etc/apt/keyrings
106新增: sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc
107新增: sudo chmod a+r /etc/apt/keyrings/docker.asc
108新增: echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/debian $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
109新增: sudo apt-get update
110新增: sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin
111新增: sudo usermod -aG docker "$(whoami)"
112新增:fi
69113
70删除:# Install Claude Code
71删除:echo "Installing Claude Code..."
72删除:curl -fsSL https://claude.ai/install.sh | bash
73删除:echo 'export PATH=$HOME/.local/bin:$HOME/.claude/local/bin:$PATH' >> ~/.zshrc
74删除:echo 'export PS1="vm:%1~%% "' >> ~/.zshrc
114新增:if [[ "$INSTALL_NODE" == "1" ]]; then
115新增: # Install Node.js 24 LTS (needed for MCP servers and Codex CLI)
116新增: echo "Installing Node.js 24..."
117新增: curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash -
118新增: sudo apt-get install -y nodejs
119新增:fi
75120
76删除:# Install OpenCode
77删除:echo "Installing OpenCode..."
78删除:curl -fsSL https://opencode.ai/install | bash
79删除:echo 'export PATH=$HOME/.opencode/bin:$PATH' >> ~/.zshrc
121新增:if [[ "$INSTALL_CHROMIUM" == "1" ]]; then
122新增: # Install Chromium and dependencies for headless browsing
123新增: echo "Installing Chromium..."
124新增: sudo apt-get install -y chromium fonts-liberation xvfb
125新增: sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome
126新增: sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome-stable
127新增: sudo mkdir -p /opt/google/chrome
128新增: sudo ln -sf /usr/bin/chromium /opt/google/chrome/chrome
129新增:fi
80130
81删除:# Add PATH to .zshenv so non-interactive shells (limactl shell vmname cmd) also find the tools
82删除:echo 'export PATH=$HOME/.local/bin:$HOME/.claude/local/bin:$HOME/.opencode/bin:$PATH' >> ~/.zshenv
131新增:if [[ "$INSTALL_GH" == "1" ]]; then
132新增: # Install GitHub CLI from official repo
133新增: echo "Installing GitHub CLI..."
134新增: sudo mkdir -p -m 755 /etc/apt/keyrings
135新增: wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null
136新增: sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg
137新增: echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null
138新增: sudo apt-get update
139新增: sudo apt-get install -y gh
140新增:fi
83141
84删除:# Install Codex CLI
85删除:echo "Installing Codex CLI..."
86删除:sudo npm i -g @openai/codex
142新增:if [[ "$INSTALL_CLAUDE" == "1" ]]; then
143新增: echo "Installing Claude Code..."
144新增: curl -fsSL https://claude.ai/install.sh | bash
145新增: echo 'export PATH=$HOME/.claude/local/bin:$PATH' >> ~/.zshrc
146新增: echo 'export PATH=$HOME/.claude/local/bin:$PATH' >> ~/.zshenv
147新增:fi
87148
88删除:# Install Mistral Vibe (installs uv and the `vibe`/`vibe-acp` commands into ~/.local/bin).
89删除:# This setup script runs under bash, but the PATH export lives in ~/.zshrc/~/.zshenv, so
90删除:# ~/.local/bin isn't on PATH here yet. The Vibe installer exits non-zero (aborting setup
91删除:# under `set -e`) if it can't find its install dir on PATH, so export it for this session.
92删除:echo "Installing Mistral Vibe..."
93删除:export PATH="$HOME/.local/bin:$PATH"
94删除:curl -LsSf https://mistral.ai/vibe/install.sh | bash
95删除:
96删除:# Configure Chrome DevTools MCP server for Claude
97删除:echo "Configuring Chrome MCP server for Claude..."
98删除:CONFIG="$HOME/.claude.json"
99删除:if [ -f "$CONFIG" ]; then
100删除: jq '.mcpServers["chrome-devtools"] = {
101删除: "command": "npx",
102删除: "args": ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"]
103删除: }' "$CONFIG" > "$CONFIG.tmp" && mv "$CONFIG.tmp" "$CONFIG"
104删除:else
105删除: cat > "$CONFIG" << 'JSON'
149新增:if [[ "$INSTALL_OPENCODE" == "1" ]]; then
150新增: echo "Installing OpenCode..."
151新增: curl -fsSL https://opencode.ai/install | bash
152新增: echo 'export PATH=$HOME/.opencode/bin:$PATH' >> ~/.zshrc
153新增: echo 'export PATH=$HOME/.opencode/bin:$PATH' >> ~/.zshenv
154新增:fi
155新增:
156新增:if [[ "$INSTALL_CODEX" == "1" ]]; then
157新增: if [[ "$INSTALL_NODE" != "1" ]]; then
158新增: echo "Skipping Codex CLI: requires Node.js (re-run setup with Node.js enabled)." >&2
159新增: else
160新增: echo "Installing Codex CLI..."
161新增: sudo npm i -g @openai/codex
162新增: fi
163新增:fi
164新增:
165新增:if [[ "$INSTALL_VIBE" == "1" ]]; then
166新增: # Vibe installs `uv` and the `vibe`/`vibe-acp` commands into ~/.local/bin.
167新增: # PATH was already exported at the top of this script so the installer
168新增: # doesn't abort on its own PATH check.
169新增: echo "Installing Mistral Vibe..."
170新增: curl -LsSf https://mistral.ai/vibe/install.sh | bash
171新增:fi
172新增:
173新增:# Chrome DevTools MCP runs via `npx` and controls the local Chromium binary, so
174新增:# only configure it when both dependencies and at least one target agent exist.
175新增:if [[ "$INSTALL_NODE" == "1" && "$INSTALL_CHROMIUM" == "1" ]]; then
176新增: if [[ "$INSTALL_CLAUDE" == "1" ]]; then
177新增: echo "Configuring Chrome MCP server for Claude..."
178新增: CONFIG="$HOME/.claude.json"
179新增: if [ -f "$CONFIG" ]; then
180新增: jq '.mcpServers["chrome-devtools"] = {
181新增: "command": "npx",
182新增: "args": ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"]
183新增: }' "$CONFIG" > "$CONFIG.tmp" && mv "$CONFIG.tmp" "$CONFIG"
184新增: else
185新增: cat > "$CONFIG" << 'JSON'
106186{
107187 "mcpServers": {
108188 "chrome-devtools": {
@@ -112,21 +192,22 @@ else
112192 }
113193}
114194JSON
115删除:fi
195新增: fi
196新增: fi
116197
117删除:# Configure Chrome DevTools MCP server for OpenCode
118删除:echo "Configuring Chrome MCP server for OpenCode..."
119删除:OPENCODE_CONFIG_DIR="$HOME/.config/opencode"
120删除:mkdir -p "$OPENCODE_CONFIG_DIR"
121删除:OPENCODE_CONFIG="$OPENCODE_CONFIG_DIR/opencode.json"
122删除:if [ -f "$OPENCODE_CONFIG" ]; then
123删除: jq '.mcp["chrome-devtools"] = {
124删除: "type": "local",
125删除: "command": ["npx", "-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"],
126删除: "enabled": true
127删除: }' "$OPENCODE_CONFIG" > "$OPENCODE_CONFIG.tmp" && mv "$OPENCODE_CONFIG.tmp" "$OPENCODE_CONFIG"
128删除:else
129删除: cat > "$OPENCODE_CONFIG" << 'JSON'
198新增: if [[ "$INSTALL_OPENCODE" == "1" ]]; then
199新增: echo "Configuring Chrome MCP server for OpenCode..."
200新增: OPENCODE_CONFIG_DIR="$HOME/.config/opencode"
201新增: mkdir -p "$OPENCODE_CONFIG_DIR"
202新增: OPENCODE_CONFIG="$OPENCODE_CONFIG_DIR/opencode.json"
203新增: if [ -f "$OPENCODE_CONFIG" ]; then
204新增: jq '.mcp["chrome-devtools"] = {
205新增: "type": "local",
206新增: "command": ["npx", "-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"],
207新增: "enabled": true
208新增: }' "$OPENCODE_CONFIG" > "$OPENCODE_CONFIG.tmp" && mv "$OPENCODE_CONFIG.tmp" "$OPENCODE_CONFIG"
209新增: else
210新增: cat > "$OPENCODE_CONFIG" << 'JSON'
130211{
131212 "$schema": "https://opencode.ai/config.json",
132213 "mcp": {
@@ -138,17 +219,18 @@ else
138219 }
139220}
140221JSON
141删除:fi
222新增: fi
223新增: fi
142224
143删除:# Configure Chrome DevTools MCP server for Vibe
144删除:# Vibe uses TOML; append an array-of-tables entry (valid even if the wizard later
145删除:# writes to the same file). Guard against duplicates on repeated setup runs.
146删除:echo "Configuring Chrome MCP server for Vibe..."
147删除:VIBE_CONFIG_DIR="$HOME/.vibe"
148删除:mkdir -p "$VIBE_CONFIG_DIR"
149删除:VIBE_CONFIG="$VIBE_CONFIG_DIR/config.toml"
150删除:if ! grep -q 'name = "chrome-devtools"' "$VIBE_CONFIG" 2>/dev/null; then
151删除: cat >> "$VIBE_CONFIG" << 'TOML'
225新增: if [[ "$INSTALL_VIBE" == "1" ]]; then
226新增: # Vibe uses TOML; append an array-of-tables entry (valid even if the wizard
227新增: # later writes to the same file). Guard against duplicates on repeated runs.
228新增: echo "Configuring Chrome MCP server for Vibe..."
229新增: VIBE_CONFIG_DIR="$HOME/.vibe"
230新增: mkdir -p "$VIBE_CONFIG_DIR"
231新增: VIBE_CONFIG="$VIBE_CONFIG_DIR/config.toml"
232新增: if ! grep -q 'name = "chrome-devtools"' "$VIBE_CONFIG" 2>/dev/null; then
233新增: cat >> "$VIBE_CONFIG" << 'TOML'
152234
153235[[mcp_servers]]
154236name = "chrome-devtools"
@@ -156,6 +238,28 @@ transport = "stdio"
156238command = "npx"
157239args = ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"]
158240TOML
241新增: fi
242新增: fi
243新增:
244新增: if [[ "$INSTALL_CODEX" == "1" ]]; then
245新增: # Codex CLI uses TOML at ~/.codex/config.toml with [mcp_servers.NAME]
246新增: # tables. Append the chrome-devtools entry; guard against duplicates on
247新增: # repeated setup runs.
248新增: echo "Configuring Chrome MCP server for Codex..."
249新增: CODEX_CONFIG_DIR="$HOME/.codex"
250新增: mkdir -p "$CODEX_CONFIG_DIR"
251新增: CODEX_CONFIG="$CODEX_CONFIG_DIR/config.toml"
252新增: if ! grep -q '\[mcp_servers\.chrome-devtools\]' "$CODEX_CONFIG" 2>/dev/null; then
253新增: cat >> "$CODEX_CONFIG" << 'TOML'
254新增:
255新增:[mcp_servers.chrome-devtools]
256新增:command = "npx"
257新增:args = ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"]
258新增:TOML
259新增: fi
260新增: fi
261新增:elif [[ "$INSTALL_CLAUDE" == "1" || "$INSTALL_OPENCODE" == "1" || "$INSTALL_CODEX" == "1" || "$INSTALL_VIBE" == "1" ]]; then
262新增: echo "Skipping Chrome MCP config: requires Node.js and Chromium." >&2
159263fi
160264
161265echo "VM setup complete."
修改agent-vm.sh+699 −49看文件
@@ -13,6 +13,7 @@
1313# agent-vm codex - Run Codex CLI in a persistent VM for cwd
1414# agent-vm vibe - Run Mistral Vibe in a persistent VM for cwd
1515# agent-vm shell - Open a shell in the persistent VM for cwd
16新增:# (alias: 'sh'; add -c "..." for a one-shot command)
1617# agent-vm stop - Stop the VM for cwd
1718# agent-vm rm - Stop and delete the VM for cwd
1819# agent-vm list - List all agent-vm VMs
@@ -24,6 +25,114 @@ AGENT_VM_TEMPLATE="agent-vm-base"
2425AGENT_VM_STATE_DIR="${HOME}/.agent-vm"
2526AGENT_VM_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
2627
28新增:# Prompt for a value with a default. Reads from /dev/tty so this still works
29新增:# when called inside command substitution. Writes the prompt to stderr and the
30新增:# answer (or the default if the user just pressed Enter) to stdout.
31新增:_agent_vm_ask() {
32新增: local prompt="$1" default="$2" reply=""
33新增: printf ' %s [%s]: ' "$prompt" "$default" >&2
34新增: IFS= read -r reply </dev/tty 2>/dev/null || reply=""
35新增: printf '%s\n' "${reply:-$default}"
36新增:}
37新增:
38新增:# Yes/no prompt. Second arg is the default: Y or N (case-insensitive). Prints
39新增:# 1 (yes) or 0 (no) to stdout. Empty input picks the default.
40新增:_agent_vm_ask_yn() {
41新增: local prompt="$1" default="${2:-Y}" reply="" indicator
42新增: case "$default" in
43新增: [Yy]*) indicator="[Y/n]"; default=Y ;;
44新增: *) indicator="[y/N]"; default=N ;;
45新增: esac
46新增: printf ' %s %s: ' "$prompt" "$indicator" >&2
47新增: IFS= read -r reply </dev/tty 2>/dev/null || reply=""
48新增: reply="${reply:-$default}"
49新增: case "$reply" in
50新增: [Yy]*) printf '1\n' ;;
51新增: *) printf '0\n' ;;
52新增: esac
53新增:}
54新增:
55新增:# Prompt for a positive integer with default. Re-prompts on invalid input.
56新增:# Used for disk/memory/cpus where a typo (e.g. "10G") would otherwise produce
57新增:# a cryptic limactl error several seconds later.
58新增:_agent_vm_ask_int() {
59新增: local prompt="$1" default="$2" reply
60新增: while true; do
61新增: reply=$(_agent_vm_ask "$prompt" "$default")
62新增: if [[ "$reply" =~ ^[1-9][0-9]*$ ]]; then
63新增: printf '%s\n' "$reply"
64新增: return 0
65新增: fi
66新增: printf ' (must be a positive integer, e.g. 10 — got: %s)\n' "$reply" >&2
67新增: done
68新增:}
69新增:
70新增:# Validate a positive-integer arg from the CLI (no retry — fail fast).
71新增:_agent_vm_validate_int() {
72新增: local name="$1" val="$2"
73新增: if [[ ! "$val" =~ ^[1-9][0-9]*$ ]]; then
74新增: echo "Error: $name must be a positive integer (got: '$val')" >&2
75新增: return 1
76新增: fi
77新增:}
78新增:
79新增:# Check Linux prerequisites Lima needs to spin up a QEMU+KVM VM. macOS uses
80新增:# different backends (vz/qemu-via-brew) so this is a no-op there. Returns
81新增:# non-zero with actionable install/permission hints when something's missing —
82新增:# without this, the user gets a generic `Error: Failed to create base VM` and
83新增:# has to dig into `~/.lima/<vm>/ha.stderr.log` to figure out why.
84新增:_agent_vm_check_linux_prereqs() {
85新增: [[ "$(uname -s)" == "Linux" ]] || return 0
86新增:
87新增: local arch_bin arch_pkg
88新增: case "$(uname -m)" in
89新增: x86_64) arch_bin="qemu-system-x86_64"; arch_pkg="qemu-system-x86" ;;
90新增: aarch64|arm64) arch_bin="qemu-system-aarch64"; arch_pkg="qemu-system-arm" ;;
91新增: *) arch_bin="qemu-system-$(uname -m)"; arch_pkg="qemu-system" ;;
92新增: esac
93新增:
94新增: local errs=0
95新增:
96新增: if ! command -v "$arch_bin" &>/dev/null; then
97新增: echo "Error: Lima needs '$arch_bin' on PATH (not found)." >&2
98新增: echo " Install with: sudo apt-get install $arch_pkg" >&2
99新增: errs=1
100新增: fi
101新增:
102新增: if [[ ! -e /dev/kvm ]]; then
103新增: echo "Error: /dev/kvm does not exist (KVM unavailable)." >&2
104新增: echo " Hardware virtualization may be disabled in BIOS, or the kernel" >&2
105新增: echo " lacks KVM support (nested virt in a guest VM, etc.)." >&2
106新增: errs=1
107新增: elif [[ ! -r /dev/kvm || ! -w /dev/kvm ]]; then
108新增: echo "Error: /dev/kvm exists but you don't have read/write access." >&2
109新增: if ! id -nG 2>/dev/null | grep -qw kvm; then
110新增: echo " Fix: sudo usermod -aG kvm \"\$USER\"" >&2
111新增: echo " Then log out and back in (or run 'newgrp kvm') so the new" >&2
112新增: echo " group membership takes effect." >&2
113新增: else
114新增: echo " You're already in the kvm group but /dev/kvm denies access." >&2
115新增: echo " Check ownership/mode: ls -l /dev/kvm" >&2
116新增: fi
117新增: errs=1
118新增: fi
119新增:
120新增: [[ $errs -eq 0 ]]
121新增:}
122新增:
123新增:# Lima leaves a partial state dir behind if `limactl create` is interrupted
124新增:# (Ctrl-C before lima.yaml is written). After that every subsequent limactl
125新增:# call on that name dies with `open ~/.lima/<vm>/lima.yaml: no such file or
126新增:# directory` — pre-emptively clean the dir so the next setup/start works.
127新增:_agent_vm_clean_partial_state() {
128新增: local vm_name="$1"
129新增: local lima_dir="$HOME/.lima/$vm_name"
130新增: if [[ -d "$lima_dir" ]] && [[ ! -f "$lima_dir/lima.yaml" ]]; then
131新增: echo "Detected partial VM state at $lima_dir (no lima.yaml) — cleaning up." >&2
132新增: rm -rf "$lima_dir"
133新增: fi
134新增:}
135新增:
27136# Generate a deterministic VM name for a directory
28137_agent_vm_name() {
29138 local dir="${1:-$(pwd)}"
@@ -44,6 +153,132 @@ _agent_vm_running() {
44153 limactl list --format '{{.Name}} {{.Status}}' 2>/dev/null | grep -q "^${1} Running$"
45154}
46155
156新增:# Stage a single host file at <dst> via hardlink, falling back to copy if the
157新增:# source and destination live on different filesystems. Hardlinking keeps the
158新增:# content live-synced with the host (same inode) without exposing the source's
159新增:# parent directory to the VM. The copy fallback preserves the no-exposure
160新增:# property but loses live sync until the next VM (re)start.
161新增:_agent_vm_stage_file() {
162新增: local src="$1" dst="$2"
163新增: mkdir -p "$(dirname "$dst")" 2>/dev/null || return 1
164新增: rm -f "$dst"
165新增: if ln "$src" "$dst" 2>/dev/null; then
166新增: return 0
167新增: fi
168新增: if cp -p "$src" "$dst" 2>/dev/null; then
169新增: echo "Warning: Staged '${src}' via copy (cross-filesystem hardlink failed); live host changes will not propagate until VM (re)start." >&2
170新增: return 0
171新增: fi
172新增: return 1
173新增:}
174新增:
175新增:# Remove all per-VM state files (version marker, terminfo cache, file mount
176新增:# cache, staging dirs). Called after a VM is deleted or before it is re-cloned
177新增:# via --reset.
178新增:_agent_vm_cleanup_state() {
179新增: local vm_name="$1"
180新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}"
181新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}"
182新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}"
183新增: rm -rf "$AGENT_VM_STATE_DIR/file-mounts/${vm_name}"
184新增:}
185新增:
186新增:# Build the .mounts JSON array for a VM. The first entry is always the project
187新增:# dir (writable). Additional entries come from ~/.agent-vm/volumes, parsed as
188新增:# Docker-Compose-ish `source[:destination][:mode]` (mode ∈ {ro,rw}, default ro).
189新增:#
190新增:# Side effects: stages any file mounts as hardlinks under
191新增:# ~/.agent-vm/file-mounts/<vm>/ and persists the file mount metadata to
192新增:# ~/.agent-vm/.agent-vm-file-mounts-<vm> so subsequent starts can re-apply the
193新增:# inside-VM bind mounts without re-parsing the volumes file. Stdout: the
194新增:# mounts JSON array (consumed by `limactl edit --set ".mounts = ..."`).
195新增:_agent_vm_build_mounts_json() {
196新增: local vm_name="$1" host_dir="$2"
197新增: local mounts_json="[{\"location\": \"${host_dir}\", \"writable\": true}"
198新增: local mounts_file="$AGENT_VM_STATE_DIR/volumes"
199新增: local file_mount_entries=()
200新增: local file_mounts_cache="$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}"
201新增:
202新增: if [[ -f "$mounts_file" ]]; then
203新增: local staging_idx=0
204新增: while IFS= read -r line || [[ -n "$line" ]]; do
205新增: line="${line%%#*}" # strip comments
206新增: line="${line#"${line%%[![:space:]]*}"}" # trim leading whitespace
207新增: line="${line%"${line##*[![:space:]]}"}" # trim trailing whitespace
208新增: [[ -z "$line" ]] && continue
209新增: # Parse source[:destination][:mode] syntax (like docker compose volumes).
210新增: # The trailing mode segment is only recognized when it equals "ro" or
211新增: # "rw" — anything else is treated as a destination path.
212新增: local src="$line" dst="" mode="ro"
213新增: if [[ "$line" == *:ro || "$line" == *:rw ]]; then
214新增: mode="${line##*:}"
215新增: line="${line%:*}"
216新增: fi
217新增: if [[ "$line" == *:* ]]; then
218新增: src="${line%%:*}"
219新增: dst="${line#*:}"
220新增: else
221新增: src="$line"
222新增: fi
223新增: src="${src/#\~/$HOME}" # expand ~
224新增: # Reject characters that would break JSON interpolation below or the
225新增: # pipe-separated cache format used for file mounts.
226新增: if [[ "$src" == *[$'"\\\n|']* || "$dst" == *[$'"\\\n|']* ]]; then
227新增: echo "Warning: Mount entry '${line}' (from ~/.agent-vm/volumes) contains invalid characters (quote/backslash/newline/pipe), skipping." >&2
228新增: continue
229新增: fi
230新增: if [[ ! -e "$src" ]]; then
231新增: echo "Warning: Mount path '${src}' (from ~/.agent-vm/volumes) does not exist, skipping." >&2
232新增: continue
233新增: fi
234新增: if [[ -f "$src" ]]; then
235新增: if [[ "$mode" == "rw" ]]; then
236新增: echo "Warning: Mount entry '${line}' (from ~/.agent-vm/volumes) requests rw on a file; only directories support rw. Mount the parent directory instead. Skipping." >&2
237新增: continue
238新增: fi
239新增: # File mount: hardlink the source into a per-VM host staging dir so
240新增: # the VM sees only this file (never the source's parent). Lima mounts
241新增: # the staging dir via virtiofs; a bind mount inside the VM (applied
242新增: # after boot) exposes the file at its final destination.
243新增: local filename
244新增: filename="$(basename "$src")"
245新增: local file_staging_dir="$AGENT_VM_STATE_DIR/file-mounts/${vm_name}/${staging_idx}"
246新增: local host_staging="${file_staging_dir}/${filename}"
247新增: if ! _agent_vm_stage_file "$src" "$host_staging"; then
248新增: echo "Warning: Failed to stage '${src}', skipping." >&2
249新增: rm -rf "$file_staging_dir"
250新增: continue
251新增: fi
252新增: local staging_mount="/tmp/.agent-vm-file-mounts/${staging_idx}"
253新增: local bind_dst="${dst:-${src}}"
254新增: file_mount_entries+=("${src}|${host_staging}|${staging_mount}/${filename}|${bind_dst}")
255新增: mounts_json+=", {\"location\": \"${file_staging_dir}\", \"mountPoint\": \"${staging_mount}\", \"writable\": false}"
256新增: staging_idx=$((staging_idx + 1))
257新增: continue
258新增: fi
259新增: if [[ ! -d "$src" ]]; then
260新增: echo "Warning: Mount path '${src}' (from ~/.agent-vm/volumes) is not a regular file or directory, skipping." >&2
261新增: continue
262新增: fi
263新增: local writable="false"
264新增: [[ "$mode" == "rw" ]] && writable="true"
265新增: if [[ -n "$dst" ]]; then
266新增: mounts_json+=", {\"location\": \"${src}\", \"mountPoint\": \"${dst}\", \"writable\": ${writable}}"
267新增: else
268新增: mounts_json+=", {\"location\": \"${src}\", \"writable\": ${writable}}"
269新增: fi
270新增: done < "$mounts_file"
271新增: fi
272新增: mounts_json+="]"
273新增:
274新增: rm -f "$file_mounts_cache"
275新增: if [[ ${#file_mount_entries[@]} -gt 0 ]]; then
276新增: printf '%s\n' "${file_mount_entries[@]}" > "$file_mounts_cache"
277新增: fi
278新增:
279新增: printf '%s' "$mounts_json"
280新增:}
281新增:
47282# Print VM resource details (CPUs, memory, disk)
48283_agent_vm_print_resources() {
49284 local vm_name="$1"
@@ -89,6 +324,11 @@ _agent_vm_ensure_running() {
89324 return 1
90325 fi
91326
327新增: # Recover from an interrupted previous run that left ~/.lima/<vm>/ without
328新增: # a lima.yaml — otherwise every limactl call on this name aborts with a
329新增: # cryptic "no such file or directory".
330新增: _agent_vm_clean_partial_state "$vm_name"
331新增:
92332 if ! limactl list -q 2>/dev/null | grep -q "^${AGENT_VM_TEMPLATE}$"; then
93333 echo "Error: Base VM not found. Run 'agent-vm setup' first." >&2
94334 return 1
@@ -99,18 +339,24 @@ _agent_vm_ensure_running() {
99339 echo "Resetting VM '$vm_name'..."
100340 limactl stop "$vm_name" &>/dev/null
101341 limactl delete "$vm_name" --force &>/dev/null
102删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}"
103删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}"
342新增: _agent_vm_cleanup_state "$vm_name"
104343 fi
105344
345新增: local is_new_vm=""
346新增: local file_mount_entries=()
347新增: local file_mounts_cache="$AGENT_VM_STATE_DIR/.agent-vm-file-mounts-${vm_name}"
348新增:
106349 if ! _agent_vm_exists "$vm_name"; then
350新增: is_new_vm=1
107351 echo "Creating VM '$vm_name'..."
108352 limactl clone "$AGENT_VM_TEMPLATE" "$vm_name" --tty=false &>/dev/null
109353 # Apply mount and resource settings via edit after clone
110354 # Mount and memory/cpus are applied separately from disk, because
111355 # Lima rejects the entire edit if disk shrinking is attempted.
356新增: local mounts_json
357新增: mounts_json=$(_agent_vm_build_mounts_json "$vm_name" "$host_dir")
112358 local edit_args=()
113删除: edit_args+=(--set ".mounts = [{\"location\": \"${host_dir}\", \"writable\": true}]")
359新增: edit_args+=(--set ".mounts = ${mounts_json}")
114360 [[ -n "$memory" ]] && edit_args+=(--memory "$memory")
115361 [[ -n "$cpus" ]] && edit_args+=(--cpus "$cpus")
116362 (cd /tmp && limactl edit "$vm_name" "${edit_args[@]}") &>/dev/null
@@ -129,9 +375,9 @@ _agent_vm_ensure_running() {
129375 # Auto-resize existing VM if --disk, --memory, or --cpus changed
130376 if _agent_vm_running "$vm_name"; then
131377 echo "VM '$vm_name' is currently running. It must be stopped to apply new resource settings."
132删除: printf "Stop the VM and apply changes? [y/N] "
133删除: local reply
134删除: read -r reply
378新增: printf "Stop the VM and apply changes? [y/N] " >&2
379新增: local reply=""
380新增: IFS= read -r reply </dev/tty 2>/dev/null || reply=""
135381 if [[ ! "$reply" =~ ^[Yy]$ ]]; then
136382 echo "Aborted. Starting with current settings."
137383 return 0
@@ -140,8 +386,9 @@ _agent_vm_ensure_running() {
140386 limactl stop "$vm_name" &>/dev/null
141387 fi
142388 echo "Updating VM resources..."
389新增: # Don't touch .mounts here — those are baked in at creation (including any
390新增: # entries from ~/.agent-vm/volumes). Re-setting them would clobber extras.
143391 local edit_args=()
144删除: edit_args+=(--set ".mounts = [{\"location\": \"${host_dir}\", \"writable\": true}]")
145392 [[ -n "$memory" ]] && edit_args+=(--memory "$memory")
146393 [[ -n "$cpus" ]] && edit_args+=(--cpus "$cpus")
147394 local edit_output
@@ -167,7 +414,14 @@ _agent_vm_ensure_running() {
167414
168415 if ! _agent_vm_running "$vm_name"; then
169416 echo "Starting VM '$vm_name'..."
170删除: limactl start "$vm_name" &>/dev/null
417新增: local start_log
418新增: if ! start_log=$(limactl start "$vm_name" 2>&1); then
419新增: echo "Error: Failed to start VM '$vm_name'." >&2
420新增: echo "--- limactl start output ---" >&2
421新增: echo "$start_log" >&2
422新增: echo "Full log: ~/.lima/$vm_name/ha.stderr.log" >&2
423新增: return 1
424新增: fi
171425 fi
172426
173427 # Sanity-check that the project directory is actually shared and writable.
@@ -184,8 +438,13 @@ _agent_vm_ensure_running() {
184438 if ! limactl shell "$vm_name" test -w "$host_dir" &>/dev/null; then
185439 echo "Project mount is not writable; repairing..." >&2
186440 limactl stop "$vm_name" &>/dev/null
441新增: # Rebuild the full mounts JSON so any ~/.agent-vm/volumes entries are
442新增: # preserved across the repair (a plain project-dir-only set would silently
443新增: # drop them).
444新增: local repair_mounts_json
445新增: repair_mounts_json=$(_agent_vm_build_mounts_json "$vm_name" "$host_dir")
187446 (cd /tmp && limactl edit "$vm_name" \
188删除: --set ".mounts = [{\"location\": \"${host_dir}\", \"writable\": true}]") &>/dev/null
447新增: --set ".mounts = ${repair_mounts_json}") &>/dev/null
189448 limactl start "$vm_name" &>/dev/null
190449 if ! limactl shell "$vm_name" test -w "$host_dir" &>/dev/null; then
191450 echo "Error: project directory is still not writable inside the VM:" >&2
@@ -210,6 +469,18 @@ _agent_vm_ensure_running() {
210469 fi
211470 fi
212471
472新增: # Push ~/.agent-vm/env (a dotenv-style file of tokens / API keys) into the VM
473新增: # at $HOME/.agent-vm.env on every start, so updates on the host propagate
474新增: # without --reset. The base VM's ~/.zshenv auto-sources it via `set -a`, so
475新增: # the contents stay a plain KEY=value file (no `export` needed). `umask 077`
476新增: # creates the file mode-600 since it usually holds secrets.
477新增: if [ -f "$AGENT_VM_STATE_DIR/env" ]; then
478新增: if ! limactl shell "$vm_name" sh -c 'umask 077 && rm -f "$HOME/.agent-vm.env" && cat > "$HOME/.agent-vm.env"' \
479新增: < "$AGENT_VM_STATE_DIR/env" 2>/dev/null; then
480新增: echo "Warning: failed to push ~/.agent-vm/env into VM '$vm_name'." >&2
481新增: fi
482新增: fi
483新增:
213484 # Run per-user runtime script if it exists
214485 if [ -f "$AGENT_VM_STATE_DIR/runtime.sh" ]; then
215486 echo "Running user runtime setup..."
@@ -234,14 +505,84 @@ _agent_vm_ensure_running() {
234505 fi
235506
236507 if [[ -n "$rdonly" ]]; then
508新增: # `mount -o remount,ro` on the host share fails silently when Lima uses
509新增: # a FUSE-backed mount (sshfs/virtiofs in some configurations) — FUSE
510新增: # rejects the `remount` option and the FS stays writable. Self-bind the
511新增: # directory first so we have a plain Linux bind-mount layer to remount,
512新增: # which works regardless of what's underneath.
237513 echo "Mounting project directory as read-only..."
238删除: limactl shell "$vm_name" sudo mount -o remount,ro "$host_dir"
514新增: if ! limactl shell "$vm_name" sudo mount --bind "$host_dir" "$host_dir" \
515新增: || ! limactl shell "$vm_name" sudo mount -o remount,ro,bind "$host_dir"; then
516新增: echo "Error: Failed to mount project directory as read-only." >&2
517新增: return 1
518新增: fi
239519 fi
240520
241521 if [[ -n "$git_ro" ]] && [[ -d "$host_dir/.git" ]]; then
242522 echo "Mounting .git directory as read-only..."
243删除: limactl shell "$vm_name" sudo mount --bind "$host_dir/.git" "$host_dir/.git"
244删除: limactl shell "$vm_name" sudo mount -o remount,ro,bind "$host_dir/.git"
523新增: if ! limactl shell "$vm_name" sudo mount --bind "$host_dir/.git" "$host_dir/.git" \
524新增: || ! limactl shell "$vm_name" sudo mount -o remount,ro,bind "$host_dir/.git"; then
525新增: echo "Error: Failed to mount .git directory as read-only." >&2
526新增: return 1
527新增: fi
528新增: fi
529新增:
530新增: # Load file mount entries from the cache. _agent_vm_build_mounts_json writes
531新增: # them there (from its own local scope) for both new and existing VMs, so we
532新增: # always read them back here to drive the inside-VM bind mounts below.
533新增: if [[ -f "$file_mounts_cache" ]]; then
534新增: local entry
535新增: while IFS= read -r entry; do
536新增: [[ -n "$entry" ]] && file_mount_entries+=("$entry")
537新增: done < "$file_mounts_cache"
538新增:
539新增: # For existing VMs, refresh host-side hardlinks so atomic-rename edits on the
540新增: # host propagate after a VM restart (ln/cp against the cached staging path).
541新增: # New VMs just staged fresh copies in _agent_vm_build_mounts_json, so there
542新增: # is nothing to refresh.
543新增: if [[ -z "$is_new_vm" ]]; then
544新增: local host_src host_staging _bind_src _bind_dst
545新增: for entry in "${file_mount_entries[@]}"; do
546新增: IFS='|' read -r host_src host_staging _bind_src _bind_dst <<< "$entry"
547新增: [[ -z "$host_staging" ]] && continue
548新增: if [[ ! -e "$host_src" ]]; then
549新增: echo "Warning: Mount source '${host_src}' no longer exists; VM will see the last-staged copy." >&2
550新增: continue
551新增: fi
552新增: _agent_vm_stage_file "$host_src" "$host_staging" \
553新增: || echo "Warning: Failed to refresh staged '${host_src}'; VM may see stale content." >&2
554新增: done
555新增: fi
556新增: fi
557新增:
558新增: # Apply inside-VM bind mounts so each staged file appears at its final path.
559新增: # Lima re-mounts staging dirs on each start, but the bind onto the final dest
560新增: # is ephemeral. Batched into one limactl shell call (roundtrips cost ~1-2s)
561新增: # and made idempotent so re-runs on a running VM are cheap no-ops.
562新增: if [[ ${#file_mount_entries[@]} -gt 0 ]]; then
563新增: local file_bind_payload=()
564新增: local _host_src _host_staging bind_src bind_dst
565新增: for entry in "${file_mount_entries[@]}"; do
566新增: IFS='|' read -r _host_src _host_staging bind_src bind_dst <<< "$entry"
567新增: [[ -n "$bind_src" && -n "$bind_dst" ]] && file_bind_payload+=("${bind_src}|${bind_dst}")
568新增: done
569新增: if [[ ${#file_bind_payload[@]} -gt 0 ]]; then
570新增: echo "Mounting individual files..."
571新增: # Paths are passed as positional args (single-quoted script) so entries
572新增: # containing quotes or metacharacters cannot be interpreted as shell code.
573新增: limactl shell "$vm_name" sudo bash -c '
574新增: set -e
575新增: for entry in "$@"; do
576新增: bind_src="${entry%%|*}"
577新增: bind_dst="${entry#*|}"
578新增: if ! findmnt -no TARGET "$bind_dst" >/dev/null 2>&1; then
579新增: mkdir -p "$(dirname "$bind_dst")" && touch "$bind_dst"
580新增: mount --bind "$bind_src" "$bind_dst"
581新增: mount -o remount,ro,bind "$bind_dst"
582新增: fi
583新增: done
584新增: ' -- "${file_bind_payload[@]}"
585新增: fi
245586 fi
246587}
247588
@@ -309,7 +650,7 @@ agent-vm() {
309650 vibe)
310651 _agent_vm_vibe "${vm_opts[@]}" "$@"
311652 ;;
312删除: shell)
653新增: shell|sh)
313654 _agent_vm_shell "${vm_opts[@]}" "$@"
314655 ;;
315656 run)
@@ -351,8 +692,11 @@ Commands:
351692 opencode [args] Run OpenCode in the VM for the current directory
352693 codex [args] Run Codex CLI in the VM for the current directory
353694 vibe [args] Run Mistral Vibe in the VM for the current directory
354删除: shell Open a shell in the VM for the current directory
355删除: run <cmd> [args] Run a command in the VM for the current directory
695新增: shell, sh Open a shell in the VM. Add -c "..." to run a one-shot
696新增: command via login zsh and exit.
697新增: run <cmd> [args] Run a command in the VM (no shell — for pipes/redirects
698新增: use 'shell -c "..."' instead; pass --tty for TUIs like
699新增: opencode, vibe, htop, etc.)
356700 stop Stop the VM for the current directory
357701 rm Stop and delete the VM for the current directory
358702 destroy-all Stop and delete all agent-vm VMs
@@ -383,13 +727,19 @@ Examples:
383727 agent-vm --readonly shell # Read-only project mount
384728 agent-vm --git-ro claude # Protect .git from writes
385729 agent-vm shell # Shell into the VM
730新增: agent-vm sh -c "ls -la | grep config" # One-shot command via login zsh
386731 agent-vm run npm install # Run a command in the VM
732新增: agent-vm run --tty opencode -p "..." # Run a TUI with PTY allocated
387733 agent-vm claude -p "fix lint errors" # Pass args to claude
388734
389735VMs are persistent and unique per directory. Running "agent-vm shell" or
390736"agent-vm claude" in the same directory will reuse the same VM.
391737
392738Customization:
739新增: ~/.agent-vm/env Shared env vars / tokens (dotenv-style;
740新增: auto-loaded into every VM shell)
741新增: ~/.agent-vm/volumes Extra host paths to mount in VMs (one per
742新增: line, supports both directories and files)
393743 ~/.agent-vm/setup.sh Per-user setup (runs during "agent-vm setup")
394744 ~/.agent-vm/runtime.sh Per-user runtime (runs on each VM start)
395745 <project>/.agent-vm.runtime.sh Per-project runtime (runs on each VM start)
@@ -402,55 +752,169 @@ _agent_vm_setup() {
402752 local disk=10
403753 local memory=3
404754 local cpus=1
755新增: local preinstall=""
756新增: local preinstall_seen=""
757新增: # Defaults match the "default install" set: every component on EXCEPT the
758新增: # opt-in languages (Ruby, Rust, Go). These apply when --preinstall isn't
759新增: # passed and either the wizard's first prompt is accepted or stdin is not a
760新增: # terminal (e.g. CI). `--preinstall=all` turns everything on;
761新增: # `--preinstall=default,rust` composes the default set with an opt-in.
762新增: local install_python=1 install_node=1
763新增: local install_ruby=0 install_rust=0 install_golang=0
764新增: local install_docker=1 install_chromium=1 install_gh=1
765新增: local install_claude=1 install_opencode=1 install_codex=1 install_vibe=1
405766
406767 while [[ $# -gt 0 ]]; do
407768 case "$1" in
408769 --help|-h)
409删除: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N]"
410删除: echo ""
411删除: echo "Create a base VM template with dev tools and agents pre-installed."
412删除: echo ""
413删除: echo "Options:"
414删除: echo " --disk GB VM disk size (default: 10)"
415删除: echo " --memory GB VM memory (default: 3)"
416删除: echo " --cpus N Number of CPUs (default: 1)"
417删除: echo " --help Show this help"
770新增: cat << 'EOF'
771新增:Usage: agent-vm setup [options]
772新增:
773新增:Create a base VM template with dev tools and agents pre-installed. Runs an
774新增:interactive wizard by default; the first prompt offers a "default install"
775新增:(everything except the opt-in languages Ruby, Rust, Go). Answer 'n' for
776新增:per-component prompts. Pass --preinstall=... to skip the wizard and pick a
777新增:specific subset non-interactively. When stdin is not a terminal (e.g. CI),
778新增:the wizard is skipped automatically and the default set is installed.
779新增:
780新增:Options:
781新增: --disk GB VM disk size (default: 10)
782新增: --memory GB VM memory (default: 3)
783新增: --cpus N Number of CPUs (default: 1)
784新增: --preinstall=LIST Comma-separated list of tools to preinstall in the base
785新增: VM image (skips the wizard). Anything not listed is
786新增: skipped. Use:
787新增: 'default' for the default set
788新增: (everything except Ruby, Rust, Go),
789新增: 'all' for everything,
790新增: 'none' for nothing.
791新增: Available names:
792新增: python, node, ruby, rust, golang, docker, chromium,
793新增: gh, claude, opencode, codex, vibe
794新增: Selecting codex, or chromium with any AI agent, also
795新增: installs node because those paths require npm/npx.
796新增: Examples:
797新增: --preinstall=default,rust # default set plus Rust
798新增: --preinstall=python,docker,claude
799新增: --help Show this help
800新增:EOF
418801 return 0
419802 ;;
420803 --disk)
421804 disk="$2"
422805 shift 2
806新增: _agent_vm_validate_int --disk "$disk" || return 1
423807 ;;
424808 --disk=*)
425809 disk="${1#*=}"
426810 shift
811新增: _agent_vm_validate_int --disk "$disk" || return 1
427812 ;;
428813 --memory|--ram)
429814 memory="$2"
430815 shift 2
816新增: _agent_vm_validate_int --memory "$memory" || return 1
431817 ;;
432818 --memory=*|--ram=*)
433819 memory="${1#*=}"
434820 shift
821新增: _agent_vm_validate_int --memory "$memory" || return 1
435822 ;;
436823 --cpus)
437824 cpus="$2"
438825 shift 2
826新增: _agent_vm_validate_int --cpus "$cpus" || return 1
439827 ;;
440828 --cpus=*)
441829 cpus="${1#*=}"
442830 shift
831新增: _agent_vm_validate_int --cpus "$cpus" || return 1
832新增: ;;
833新增: --preinstall)
834新增: preinstall_seen=1
835新增: # Only consume the next argument as the value when it is an actual
836新增: # value, not another option. Bare `--preinstall` falls back to the
837新增: # default set (handled below) without swallowing e.g. a following
838新增: # `--disk`.
839新增: if [[ -n "$2" && "$2" != -* ]]; then
840新增: preinstall="$2"
841新增: shift 2
842新增: else
843新增: shift
844新增: fi
845新增: ;;
846新增: --preinstall=*)
847新增: preinstall="${1#*=}"
848新增: preinstall_seen=1
849新增: shift
443850 ;;
444851 --reset|--offline|--readonly|--git-read-only|--git-ro)
445852 shift ;;
446853 *)
447854 echo "Unknown option: $1" >&2
448删除: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N]" >&2
855新增: echo "Usage: agent-vm setup [--disk GB] [--memory GB] [--cpus N] [--preinstall=LIST]" >&2
449856 return 1
450857 ;;
451858 esac
452859 done
453860
861新增: # Apply --preinstall=LIST: start with everything OFF and turn on what's
862新增: # listed. 'default' / 'all' / 'none' are shortcuts. Setting this also
863新增: # bypasses the interactive wizard below. `--preinstall=` with no value
864新增: # (or `--preinstall` swallowed by a following flag) falls back to the
865新增: # default set — explicitly opting into the wizard's recommended install.
866新增: # Pass `--preinstall=none` if you really want nothing.
867新增: if [[ -n "$preinstall_seen" ]]; then
868新增: install_python=0 install_node=0 install_ruby=0 install_rust=0 install_golang=0
869新增: install_docker=0 install_chromium=0 install_gh=0
870新增: install_claude=0 install_opencode=0 install_codex=0 install_vibe=0
871新增: [[ -z "$preinstall" ]] && preinstall="default"
872新增: # Iterate the comma-list portably across bash and zsh by appending a
873新增: # trailing comma and peeling off one token per iteration.
874新增: local rest="${preinstall}," f
875新增: while [[ -n "$rest" ]]; do
876新增: f="${rest%%,*}"
877新增: rest="${rest#*,}"
878新增: # Trim whitespace. Use bash/zsh-portable substitutions only.
879新增: f="${f# }"; f="${f% }"
880新增: [[ -z "$f" ]] && continue
881新增: case "$f" in
882新增: all)
883新增: install_python=1 install_node=1 install_ruby=1
884新增: install_rust=1 install_golang=1
885新增: install_docker=1 install_chromium=1 install_gh=1
886新增: install_claude=1 install_opencode=1 install_codex=1 install_vibe=1
887新增: ;;
888新增: default)
889新增: install_python=1 install_node=1
890新增: install_docker=1 install_chromium=1 install_gh=1
891新增: install_claude=1 install_opencode=1 install_codex=1 install_vibe=1
892新增: ;;
893新增: none) ;; # explicit no-op token; with the all-off reset above,
894新增: # `--preinstall=none` ships nothing.
895新增: python) install_python=1 ;;
896新增: node) install_node=1 ;;
897新增: ruby) install_ruby=1 ;;
898新增: rust) install_rust=1 ;;
899新增: golang) install_golang=1 ;;
900新增: docker) install_docker=1 ;;
901新增: chromium) install_chromium=1 ;;
902新增: gh) install_gh=1 ;;
903新增: claude) install_claude=1 ;;
904新增: opencode) install_opencode=1 ;;
905新增: codex) install_codex=1 ;;
906新增: vibe) install_vibe=1 ;;
907新增: *)
908新增: echo "Unknown preinstall name: $f (names are lowercase)" >&2
909新增: echo "Valid: python, node, ruby, rust, golang, docker, chromium, gh, claude, opencode, codex, vibe, default, all, none" >&2
910新增: return 1
911新增: ;;
912新增: esac
913新增: done
914新增: fi
915新增:
916新增: # Fail-fast checks before the (potentially long) wizard so the user doesn't
917新增: # answer 15 prompts only to be told their host is missing Lima or KVM.
454918 if ! command -v limactl &>/dev/null; then
455919 if command -v brew &>/dev/null; then
456920 echo "Installing Lima..."
@@ -461,6 +925,102 @@ _agent_vm_setup() {
461925 fi
462926 fi
463927
928新增: _agent_vm_check_linux_prereqs || return 1
929新增:
930新增: # Interactive wizard, unless --preinstall was passed or no terminal is
931新增: # attached (e.g. running under CI). Defaults shown in [] are prefilled from
932新增: # any --disk/--memory/--cpus flags the user already passed, so they can
933新增: # confirm or override. Components default to the "default install" set
934新增: # (everything except Ruby/Rust/Go). The first prompt offers that whole set
935新增: # as a one-tap shortcut — answer 'n' for per-component prompts.
936新增: if [[ -z "$preinstall_seen" ]] && [[ -r /dev/tty ]]; then
937新增: printf '\nagent-vm setup wizard\n' >&2
938新增: printf '─────────────────────\n\n' >&2
939新增: printf 'These settings apply to the base VM image. Every per-project VM is\n' >&2
940新增: printf 'cloned from it, so anything preinstalled here is available in all\n' >&2
941新增: printf 'future agent VMs. You can still install extra tools inside any\n' >&2
942新增: printf 'individual VM later (e.g. via `agent-vm shell`).\n\n' >&2
943新增: printf 'For more: https://github.com/sylvinus/agent-vm\n\n' >&2
944新增:
945新增: # Software first — the more interesting choice for most users.
946新增: printf 'Software\n' >&2
947新增: printf '────────\n' >&2
948新增: printf ' Install: Python, Node.js, Docker, Chromium, gh,\n' >&2
949新增: printf ' Claude Code, OpenCode, Codex CLI, Mistral Vibe\n' >&2
950新增: printf ' Skip: Ruby, Rust, Go\n\n' >&2
951新增: local use_default_software
952新增: use_default_software=$(_agent_vm_ask_yn "Use this default" Y)
953新增: if [[ "$use_default_software" != "1" ]]; then
954新增: printf '\nAI coding agents\n' >&2
955新增: printf '────────────────\n' >&2
956新增: install_claude=$(_agent_vm_ask_yn "Claude Code" Y)
957新增: install_opencode=$(_agent_vm_ask_yn "OpenCode" Y)
958新增: install_codex=$(_agent_vm_ask_yn "Codex CLI" Y)
959新增: install_vibe=$(_agent_vm_ask_yn "Mistral Vibe" Y)
960新增:
961新增: printf '\nSystem tools\n' >&2
962新增: printf '────────────\n' >&2
963新增: install_docker=$(_agent_vm_ask_yn "Docker" Y)
964新增: install_chromium=$(_agent_vm_ask_yn "Chromium (headless browser)" Y)
965新增: install_gh=$(_agent_vm_ask_yn "GitHub CLI (gh)" Y)
966新增:
967新增: local node_forced_reason=""
968新增: if [[ "$install_codex" == "1" ]]; then
969新增: node_forced_reason="Codex CLI requires Node.js"
970新增: elif [[ "$install_chromium" == "1" && ( "$install_claude" == "1" || "$install_opencode" == "1" || "$install_vibe" == "1" ) ]]; then
971新增: node_forced_reason="Chrome DevTools MCP uses npx"
972新增: fi
973新增:
974新增: printf '\nLanguages\n' >&2
975新增: printf '─────────\n' >&2
976新增: install_python=$(_agent_vm_ask_yn "Python 3" Y)
977新增: if [[ -n "$node_forced_reason" ]]; then
978新增: install_node=1
979新增: printf 'Node.js 24: yes (%s)\n' "$node_forced_reason" >&2
980新增: else
981新增: install_node=$(_agent_vm_ask_yn "Node.js 24" Y)
982新增: fi
983新增: install_ruby=$(_agent_vm_ask_yn "Ruby" Y)
984新增: install_rust=$(_agent_vm_ask_yn "Rust" Y)
985新增: install_golang=$(_agent_vm_ask_yn "Go" Y)
986新增: fi
987新增:
988新增: # Resources second — same pattern, accept-in-one-go shortcut. Current
989新增: # values reflect any --disk/--memory/--cpus already passed on the CLI.
990新增: # These are starting values: any later `agent-vm` command can resize the
991新增: # per-project VM with --disk/--memory/--cpus.
992新增: printf '\nDefault resources\n' >&2
993新增: printf '─────────────────\n' >&2
994新增: printf '(per-VM override with --disk / --memory / --cpus on any agent-vm command)\n\n' >&2
995新增: printf ' Disk %s GB\n' "$disk" >&2
996新增: printf ' Memory %s GB\n' "$memory" >&2
997新增: printf ' CPUs %s\n\n' "$cpus" >&2
998新增: local use_default_resources
999新增: use_default_resources=$(_agent_vm_ask_yn "Use these defaults" Y)
1000新增: if [[ "$use_default_resources" != "1" ]]; then
1001新增: disk=$(_agent_vm_ask_int "Disk size in GB" "$disk")
1002新增: memory=$(_agent_vm_ask_int "Memory in GB" "$memory")
1003新增: cpus=$(_agent_vm_ask_int "Number of CPUs" "$cpus")
1004新增: fi
1005新增: printf '\n' >&2
1006新增: fi
1007新增:
1008新增: if [[ "$install_chromium" == "1" ]]; then
1009新增: local wants_chrome_mcp=0
1010新增: [[ "$install_claude" == "1" || "$install_opencode" == "1" || "$install_codex" == "1" || "$install_vibe" == "1" ]] && wants_chrome_mcp=1
1011新增: if [[ "$wants_chrome_mcp" == "1" && "$install_node" != "1" ]]; then
1012新增: echo "Enabling Node.js because Chrome DevTools MCP uses npx." >&2
1013新增: install_node=1
1014新增: fi
1015新增: fi
1016新增:
1017新增: if [[ "$install_codex" == "1" && "$install_node" != "1" ]]; then
1018新增: echo "Enabling Node.js because Codex CLI requires npm." >&2
1019新增: install_node=1
1020新增: fi
1021新增:
1022新增: _agent_vm_clean_partial_state "$AGENT_VM_TEMPLATE"
1023新增:
4641024 limactl stop "$AGENT_VM_TEMPLATE" &>/dev/null
4651025 limactl delete "$AGENT_VM_TEMPLATE" --force &>/dev/null
4661026
@@ -469,19 +1029,55 @@ _agent_vm_setup() {
4691029 --set '.mounts=[]'
4701030 --disk="$disk"
4711031 --memory="$memory"
1032新增: --cpus="$cpus"
4721033 --tty=false
4731034 )
474删除: [[ -n "$cpus" ]] && create_args+=(--cpus="$cpus")
475删除: limactl create --name="$AGENT_VM_TEMPLATE" template:debian-13 \
476删除: "${create_args[@]}" &>/dev/null || { echo "Error: Failed to create base VM." >&2; return 1; }
1035新增: local create_log
1036新增: if ! create_log=$(limactl create --name="$AGENT_VM_TEMPLATE" template:debian-13 "${create_args[@]}" 2>&1); then
1037新增: echo "Error: Failed to create base VM." >&2
1038新增: echo "--- limactl create output ---" >&2
1039新增: echo "$create_log" >&2
1040新增: return 1
1041新增: fi
4771042
4781043 _agent_vm_print_resources "$AGENT_VM_TEMPLATE"
4791044
480删除: limactl start "$AGENT_VM_TEMPLATE" &>/dev/null || { echo "Error: Failed to start base VM." >&2; return 1; }
1045新增: local start_log
1046新增: if ! start_log=$(limactl start "$AGENT_VM_TEMPLATE" 2>&1); then
1047新增: echo "Error: Failed to start base VM." >&2
1048新增: echo "--- limactl start output ---" >&2
1049新增: echo "$start_log" >&2
1050新增: echo "Full log: ~/.lima/$AGENT_VM_TEMPLATE/ha.stderr.log" >&2
1051新增: return 1
1052新增: fi
4811053
482删除: # Run the setup script inside the VM
1054新增: # Run the setup script inside the VM. Component selections are passed by
1055新增: # prepending `export` lines to the script on stdin — keeps the integration
1056新增: # to one knob (env vars) and avoids quoting headaches with `limactl shell
1057新增: # env KEY=VAL`. The setup script's defaults for each flag match the host
1058新增: # wizard's "default install" set (Ruby/Rust/Go off, everything else on),
1059新增: # so invoking the in-VM script standalone — without these exports — still
1060新增: # produces the same default install.
4831061 echo "Installing packages inside VM..."
484删除: limactl shell "$AGENT_VM_TEMPLATE" bash -l < "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" || { echo "Error: Setup script failed." >&2; return 1; }
1062新增: if [[ ! -r "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" ]]; then
1063新增: echo "Error: Setup script not found at ${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" >&2
1064新增: return 1
1065新增: fi
1066新增: {
1067新增: printf 'export AGENT_VM_INSTALL_PYTHON=%s\n' "$install_python"
1068新增: printf 'export AGENT_VM_INSTALL_NODE=%s\n' "$install_node"
1069新增: printf 'export AGENT_VM_INSTALL_RUBY=%s\n' "$install_ruby"
1070新增: printf 'export AGENT_VM_INSTALL_RUST=%s\n' "$install_rust"
1071新增: printf 'export AGENT_VM_INSTALL_GOLANG=%s\n' "$install_golang"
1072新增: printf 'export AGENT_VM_INSTALL_DOCKER=%s\n' "$install_docker"
1073新增: printf 'export AGENT_VM_INSTALL_CHROMIUM=%s\n' "$install_chromium"
1074新增: printf 'export AGENT_VM_INSTALL_GH=%s\n' "$install_gh"
1075新增: printf 'export AGENT_VM_INSTALL_CLAUDE=%s\n' "$install_claude"
1076新增: printf 'export AGENT_VM_INSTALL_OPENCODE=%s\n' "$install_opencode"
1077新增: printf 'export AGENT_VM_INSTALL_CODEX=%s\n' "$install_codex"
1078新增: printf 'export AGENT_VM_INSTALL_VIBE=%s\n' "$install_vibe"
1079新增: cat "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh"
1080新增: } | limactl shell "$AGENT_VM_TEMPLATE" bash -l || { echo "Error: Setup script failed." >&2; return 1; }
4851081
4861082 # Run user's custom setup script if it exists
4871083 local user_setup="$AGENT_VM_STATE_DIR/setup.sh"
@@ -497,7 +1093,13 @@ _agent_vm_setup() {
4971093 date +%s > "$AGENT_VM_STATE_DIR/.agent-vm-base-version"
4981094
4991095 echo ""
500删除: echo "Base VM ready. Run 'agent-vm shell', 'agent-vm claude', 'agent-vm opencode', 'agent-vm codex', or 'agent-vm vibe' in any project directory."
1096新增: echo "Base VM ready. Try one of these in any project directory:"
1097新增: echo " agent-vm shell"
1098新增: [[ "$install_claude" == "1" ]] && echo " agent-vm claude"
1099新增: [[ "$install_opencode" == "1" ]] && echo " agent-vm opencode"
1100新增: [[ "$install_codex" == "1" ]] && echo " agent-vm codex"
1101新增: [[ "$install_vibe" == "1" ]] && echo " agent-vm vibe"
1102新增: echo ""
5011103 echo "Note: Existing VMs were not updated. Use --reset to re-clone them from the new base."
5021104}
5031105
@@ -527,7 +1129,7 @@ _agent_vm_claude() {
5271129 _agent_vm_print_resources "$vm_name"
5281130
5291131 local exit_code=0
530删除: limactl shell --workdir "$host_dir" "$vm_name" claude --dangerously-skip-permissions "${args[@]}"
1132新增: limactl shell --workdir "$host_dir" "$vm_name" -- claude --dangerously-skip-permissions "${args[@]}"
5311133 exit_code=$?
5321134 [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; }
5331135 return $exit_code
@@ -636,6 +1238,7 @@ _agent_vm_vibe() {
6361238_agent_vm_shell() {
6371239 local vm_opts=()
6381240 local rm=""
1241新增: local cmd_string=""
6391242 while [[ $# -gt 0 ]]; do
6401243 case "$1" in
6411244 --disk) vm_opts+=(--disk "$2"); shift 2 ;;
@@ -646,6 +1249,12 @@ _agent_vm_shell() {
6461249 --readonly) vm_opts+=(--readonly); shift ;;
6471250 --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;;
6481251 --rm) rm=1; shift ;;
1252新增: -c|--command)
1253新增: if [[ $# -lt 2 || -z "$2" ]]; then
1254新增: echo "Error: -c/--command requires a command string." >&2
1255新增: return 1
1256新增: fi
1257新增: cmd_string="$2"; shift 2 ;;
6491258 *) shift ;;
6501259 esac
6511260 done
@@ -657,15 +1266,22 @@ _agent_vm_shell() {
6571266 _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1
6581267 _agent_vm_print_resources "$vm_name"
6591268
660删除: echo "VM: $vm_name | Dir: $host_dir"
661删除: if [[ -n "$rm" ]]; then
662删除: echo "Type 'exit' to leave. VM will be destroyed after exit."
1269新增: local exit_code=0
1270新增: if [[ -n "$cmd_string" ]]; then
1271新增: # One-shot: run the command via a login zsh so ~/.zshenv (mise, PATH,
1272新增: # ~/.agent-vm.env, …) is loaded. No "Type 'exit'..." chatter.
1273新增: limactl shell --workdir "$host_dir" "$vm_name" zsh -l -c "$cmd_string"
1274新增: exit_code=$?
6631275 else
664删除: echo "Type 'exit' to leave (VM keeps running). Use 'agent-vm stop' to stop it."
1276新增: echo "VM: $vm_name | Dir: $host_dir"
1277新增: if [[ -n "$rm" ]]; then
1278新增: echo "Type 'exit' to leave. VM will be destroyed after exit."
1279新增: else
1280新增: echo "Type 'exit' to leave (VM keeps running). Use 'agent-vm stop' to stop it."
1281新增: fi
1282新增: limactl shell --workdir "$host_dir" "$vm_name" zsh -l
1283新增: exit_code=$?
6651284 fi
666删除: local exit_code=0
667删除: limactl shell --workdir "$host_dir" "$vm_name" zsh -l
668删除: exit_code=$?
6691285 [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; }
6701286 return $exit_code
6711287}
@@ -674,6 +1290,7 @@ _agent_vm_run() {
6741290 local vm_opts=()
6751291 local args=()
6761292 local rm=""
1293新增: local tty_flag=""
6771294 while [[ $# -gt 0 ]]; do
6781295 case "$1" in
6791296 --disk) vm_opts+=(--disk "$2"); shift 2 ;;
@@ -684,11 +1301,12 @@ _agent_vm_run() {
6841301 --readonly) vm_opts+=(--readonly); shift ;;
6851302 --git-read-only|--git-ro) vm_opts+=(--git-read-only); shift ;;
6861303 --rm) rm=1; shift ;;
1304新增: --tty) tty_flag=1; shift ;;
6871305 *) args+=("$1"); shift ;;
6881306 esac
6891307 done
6901308 if [[ ${#args[@]} -eq 0 ]]; then
691删除: echo "Usage: agent-vm run <command> [args]" >&2
1309新增: echo "Usage: agent-vm run [--tty] <command> [args]" >&2
6921310 return 1
6931311 fi
6941312 local host_dir
@@ -699,8 +1317,15 @@ _agent_vm_run() {
6991317 _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1
7001318 _agent_vm_print_resources "$vm_name"
7011319
1320新增: # `--tty` forces limactl to allocate a pseudo-terminal in the VM, which
1321新增: # full-screen TUIs (opencode, vibe, htop, …) need to render correctly when
1322新增: # invoked through `agent-vm run`. Without it, line-mode tools work fine but
1323新增: # ncurses-style UIs break.
1324新增: local shell_opts=(--workdir "$host_dir")
1325新增: [[ -n "$tty_flag" ]] && shell_opts+=(--tty)
1326新增:
7021327 local exit_code=0
703删除: limactl shell --workdir "$host_dir" "$vm_name" "${args[@]}"
1328新增: limactl shell "${shell_opts[@]}" "$vm_name" "${args[@]}"
7041329 exit_code=$?
7051330 [[ -n "$rm" ]] && { echo "Removing VM..."; _agent_vm_destroy; }
7061331 return $exit_code
@@ -736,8 +1361,7 @@ _agent_vm_destroy() {
7361361 echo "Stopping and deleting VM '$vm_name'..."
7371362 limactl stop "$vm_name" &>/dev/null
7381363 limactl delete "$vm_name" --force &>/dev/null
739删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}"
740删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm_name}"
1364新增: _agent_vm_cleanup_state "$vm_name"
7411365 echo "VM destroyed."
7421366}
7431367
@@ -750,9 +1374,9 @@ _agent_vm_destroy_all() {
7501374 fi
7511375 echo "This will destroy the following VMs:"
7521376 echo "$vms"
753删除: printf "Continue? [y/N] "
754删除: local reply
755删除: read -r reply
1377新增: printf "Continue? [y/N] " >&2
1378新增: local reply=""
1379新增: IFS= read -r reply </dev/tty 2>/dev/null || reply=""
7561380 if [[ ! "$reply" =~ ^[Yy]$ ]]; then
7571381 echo "Aborted."
7581382 return 0
@@ -761,8 +1385,7 @@ _agent_vm_destroy_all() {
7611385 echo "Destroying $vm..."
7621386 limactl stop "$vm" &>/dev/null
7631387 limactl delete "$vm" --force &>/dev/null
764删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm}"
765删除: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-term-${vm}"
1388新增: _agent_vm_cleanup_state "$vm"
7661389 done
7671390 echo "All VMs destroyed."
7681391}
@@ -794,3 +1417,30 @@ _agent_vm_status() {
7941417 fi
7951418 done || echo " (no VMs)"
7961419}
1420新增:
1421新增:# When the file is executed directly (`./agent-vm.sh setup`) rather than
1422新增:# sourced, dispatch to the agent-vm function so it doesn't silently no-op.
1423新增:# Sourcing remains the canonical install path because it makes `agent-vm`
1424新增:# available as a shell function across all future commands. Detection is
1425新增:# shell-specific:
1426新增:# bash: BASH_SOURCE[0] differs from $0 when sourced
1427新增:# zsh: ZSH_EVAL_CONTEXT contains ':file' when sourced
1428新增:_agent_vm_is_sourced() {
1429新增: if [[ -n "${BASH_VERSION:-}" ]]; then
1430新增: [[ "${BASH_SOURCE[0]}" != "$0" ]]
1431新增: elif [[ -n "${ZSH_VERSION:-}" ]]; then
1432新增: [[ "${ZSH_EVAL_CONTEXT:-}" == *:file* ]]
1433新增: else
1434新增: return 0 # unknown shell — assume sourced and don't auto-run
1435新增: fi
1436新增:}
1437新增:
1438新增:if ! _agent_vm_is_sourced; then
1439新增: unset -f _agent_vm_is_sourced
1440新增: agent-vm "$@"
1441新增: # Preserve agent-vm's exit code — without the explicit exit, the script
1442新增: # would end on the implicit `unset -f` below, which always returns 0 and
1443新增: # would mask command failures (`./agent-vm.sh bad-cmd; echo $?` → 0).
1444新增: exit $?
1445新增:fi
1446新增:unset -f _agent_vm_is_sourced
修改runtime.example.sh+2 −0看文件
@@ -47,6 +47,8 @@
4747# =============================================================================
4848#
4949# Required for creating PRs, commenting on issues, etc. from inside the VM.
50新增:# (If you only need `gh` for CLI calls, setting GH_TOKEN in ~/.agent-vm/env is
51新增:# simpler — see "Sharing tokens" in the README.)
5052#
5153# To create a token: https://github.com/settings/tokens
5254# Scopes needed: repo, read:org