提交记录修改README.md+123 −39看文件
新增agent-vm.setup.sh+101 −0看文件
新增agent-vm.sh+461 −0看文件
删除claude-vm.sh+0 −247
| @@ -4,7 +4,9 @@ Run AI coding agents inside sandboxed Linux VMs. The agent gets full autonomy wh | ||
| 4 | 4 | |
| 5 | 5 | Uses [Lima](https://lima-vm.io/) to create lightweight Debian VMs on macOS and Linux. Ships with dev tools, Docker, and a headless Chrome browser with [Chrome DevTools MCP](https://github.com/ChromeDevTools/chrome-devtools-mcp) pre-configured. |
| 6 | 6 | |
| 7 | 删除:Currently supports [Claude Code](https://claude.ai/code). Other agents (Codex, etc.) can be added in the future. | |
| 7 | 新增:Supports [Claude Code](https://claude.ai/code), [OpenCode](https://github.com/opencode-ai/opencode), and [Codex CLI](https://github.com/openai/codex) out of the box. Other agents can be run via `agent-vm shell`. | |
| 8 | 新增: | |
| 9 | 新增:Never install attack vectors such as npm, claude or even Docker on your host machine again! | |
| 8 | 10 | |
| 9 | 11 | Feedbacks welcome! |
| 10 | 12 | |
| @@ -12,7 +14,7 @@ Feedbacks welcome! | ||
| 12 | 14 | |
| 13 | 15 | - macOS or Linux |
| 14 | 16 | - [Lima](https://lima-vm.io/docs/installation/) (installed automatically via Homebrew if available) |
| 15 | 删除:- A [Claude subscription](https://claude.ai/) (Pro, Max, or Team) | |
| 17 | 新增:- A subscription or API key for your agent of choice | |
| 16 | 18 | |
| 17 | 19 | ## Install |
| 18 | 20 | |
| @@ -21,8 +23,8 @@ git clone https://github.com/sylvinus/agent-vm.git | ||
| 21 | 23 | cd agent-vm |
| 22 | 24 | |
| 23 | 25 | # Add to your shell config |
| 24 | 删除:echo "source $(pwd)/claude-vm.sh" >> ~/.zshrc # zsh | |
| 25 | 删除:echo "source $(pwd)/claude-vm.sh" >> ~/.bashrc # or bash | |
| 26 | 新增:echo "source $(pwd)/agent-vm.sh" >> ~/.zshrc # zsh | |
| 27 | 新增:echo "source $(pwd)/agent-vm.sh" >> ~/.bashrc # or bash | |
| 26 | 28 | ``` |
| 27 | 29 | |
| 28 | 30 | ## Usage |
| @@ -30,94 +32,178 @@ echo "source $(pwd)/claude-vm.sh" >> ~/.bashrc # or bash | ||
| 30 | 32 | ### One-time setup |
| 31 | 33 | |
| 32 | 34 | ```bash |
| 33 | 删除:claude-vm-setup | |
| 35 | 新增:agent-vm setup | |
| 34 | 36 | ``` |
| 35 | 37 | |
| 36 | 删除:Creates a VM template with dev tools, Docker, Chromium, and Claude Code pre-installed. During setup, Claude will launch once for authentication. After it responds, type `/exit` to continue with the rest of the setup. (We haven't found a way to automate this step yet.) | |
| 38 | 新增:Creates a base VM template with dev tools, Docker, Chromium, and AI coding agents pre-installed. | |
| 37 | 39 | |
| 38 | 40 | Options: |
| 39 | 41 | |
| 40 | 42 | | Flag | Description | Default | |
| 41 | 43 | |------|-------------|---------| |
| 42 | 删除:| `--minimal` | Only install git, curl, jq, and Claude Code. Skips Docker, Node.js, Python, Chromium, and the Chrome MCP server. | off | | |
| 43 | 44 | | `--disk GB` | VM disk size in GB | 20 | |
| 44 | 45 | | `--memory GB` | VM memory in GB | 8 | |
| 45 | 46 | |
| 46 | 47 | ```bash |
| 47 | 删除:claude-vm-setup --minimal # Lightweight VM with just Claude | |
| 48 | 删除:claude-vm-setup --disk 50 --memory 16 # Larger VM for heavy workloads | |
| 48 | 新增:agent-vm setup --disk 50 --memory 16 # Larger VM for heavy workloads | |
| 49 | 49 | ``` |
| 50 | 50 | |
| 51 | 删除:### Run Claude in a VM | |
| 51 | 新增:### Run an agent in a VM | |
| 52 | 52 | |
| 53 | 53 | ```bash |
| 54 | 54 | cd your-project |
| 55 | 删除:claude-vm | |
| 55 | 新增:agent-vm claude # Claude Code | |
| 56 | 新增:agent-vm opencode # OpenCode | |
| 57 | 新增:agent-vm codex # Codex CLI | |
| 58 | 新增:``` | |
| 59 | 新增: | |
| 60 | 新增:Creates a persistent VM for the current directory (or reuses it if one already exists), mounts your working directory, and runs the agent with full permissions. The VM persists after the agent exits so you can reconnect later. | |
| 61 | 新增: | |
| 62 | 新增:Each agent runs with its respective auto-approve flag: | |
| 63 | 新增:- `claude` runs with `--dangerously-skip-permissions` | |
| 64 | 新增:- `opencode` runs with `--dangerously-skip-permissions` | |
| 65 | 新增:- `codex` runs with `--full-auto` | |
| 66 | 新增: | |
| 67 | 新增:Any extra arguments are forwarded to the agent command: | |
| 68 | 新增: | |
| 69 | 新增:```bash | |
| 70 | 新增:agent-vm claude -p "fix all lint errors" # Run with a prompt | |
| 71 | 新增:agent-vm claude --resume # Resume previous session | |
| 72 | 新增:agent-vm opencode -p "refactor auth module" # OpenCode with a prompt | |
| 73 | 新增:agent-vm codex -q "explain this codebase" # Codex with a query | |
| 74 | 新增:``` | |
| 75 | 新增: | |
| 76 | 新增:### Shell access | |
| 77 | 新增: | |
| 78 | 新增:```bash | |
| 79 | 新增:agent-vm shell | |
| 56 | 80 | ``` |
| 57 | 81 | |
| 58 | 删除:Clones the template into a fresh VM, mounts your current directory, and runs `claude --dangerously-skip-permissions`. The VM is deleted when Claude exits. | |
| 82 | 新增:Opens a zsh shell in the same persistent VM for the current directory. Useful for debugging, running commands alongside an agent, or using agents not built in. | |
| 83 | 新增: | |
| 84 | 新增:### VM lifecycle | |
| 59 | 85 | |
| 60 | 删除:Any arguments passed to `claude-vm` are forwarded to the `claude` command: | |
| 86 | 新增:Each directory gets its own persistent VM. You can manage it with: | |
| 61 | 87 | |
| 62 | 88 | ```bash |
| 63 | 删除:claude-vm -p "fix all lint errors" # Run with a prompt | |
| 64 | 删除:claude-vm --resume # Resume previous session | |
| 65 | 删除:claude-vm -c "explain this codebase" # Continue conversation | |
| 89 | 新增:agent-vm status # Show VM status for the current directory | |
| 90 | 新增:agent-vm stop # Stop the VM (can be restarted later) | |
| 91 | 新增:agent-vm destroy # Stop and permanently delete the VM | |
| 66 | 92 | ``` |
| 67 | 93 | |
| 68 | 删除:### Debug shell | |
| 94 | 新增:To resize an existing VM's disk or memory, just pass `--disk` or `--memory` again — the VM will be stopped, reconfigured, and restarted automatically: | |
| 69 | 95 | |
| 70 | 96 | ```bash |
| 71 | 删除:claude-vm-shell | |
| 97 | 新增:agent-vm --disk 50 claude # Grow disk to 50GB, then run Claude | |
| 98 | 新增:agent-vm --memory 16 shell # Increase memory to 16GB, then open shell | |
| 72 | 99 | ``` |
| 73 | 100 | |
| 74 | 删除:Same as `claude-vm` but drops you into a bash shell instead. | |
| 101 | 新增:Note: disk can only be grown, not shrunk. | |
| 102 | 新增: | |
| 103 | 新增:Running `agent-vm setup` again updates the base template but does **not** update existing VMs. You'll see a warning when using a VM cloned from an older base. Use `--reset` to re-clone: | |
| 104 | 新增: | |
| 105 | 新增:```bash | |
| 106 | 新增:agent-vm --reset claude # Destroy and re-clone VM, then run Claude | |
| 107 | 新增:``` | |
| 75 | 108 | |
| 76 | 109 | ## Customization |
| 77 | 110 | |
| 78 | 删除:### Per-user: `~/.claude-vm.setup.sh` | |
| 111 | 新增:### Per-user: `~/.agent-vm.setup.sh` | |
| 79 | 112 | |
| 80 | 删除:Create this file in your home directory to install extra tools into the VM template. It runs once during `claude-vm-setup`, as the default VM user (with sudo available): | |
| 113 | 新增:Create this file in your home directory to install extra tools into the base VM template. It runs once during `agent-vm setup`, as the default VM user (with sudo available): | |
| 81 | 114 | |
| 82 | 115 | ```bash |
| 83 | 删除:# ~/.claude-vm.setup.sh | |
| 116 | 新增:# ~/.agent-vm.setup.sh | |
| 84 | 117 | sudo apt-get install -y postgresql-client |
| 85 | 118 | pip install pandas numpy |
| 86 | 119 | ``` |
| 87 | 120 | |
| 88 | 删除:### Per-project: `.claude-vm.runtime.sh` | |
| 121 | 新增:### Per-project: `.agent-vm.runtime.sh` | |
| 89 | 122 | |
| 90 | 删除:Create this file at the root of any project. It runs inside the cloned VM each time you call `claude-vm`, just before Claude starts. Use it for project-specific setup like installing dependencies or starting services: | |
| 123 | 新增:Create this file at the root of any project. It runs inside the VM each time a new VM is created for the project, just before you get access. Use it for project-specific setup like installing dependencies or starting services: | |
| 91 | 124 | |
| 92 | 125 | ```bash |
| 93 | 删除:# your-project/.claude-vm.runtime.sh | |
| 126 | 新增:# your-project/.agent-vm.runtime.sh | |
| 94 | 127 | npm install |
| 95 | 128 | docker compose up -d |
| 96 | 129 | ``` |
| 97 | 130 | |
| 131 | 新增:### MCP servers | |
| 132 | 新增: | |
| 133 | 新增:The base VM comes with [Chrome DevTools MCP](https://github.com/ChromeDevTools/chrome-devtools-mcp) pre-configured for Claude, giving the agent headless browser access. | |
| 134 | 新增: | |
| 135 | 新增:To add more MCP servers, add them to `~/.claude.json` in your `~/.agent-vm.setup.sh`, or edit the file directly inside a VM via `agent-vm shell`. Add entries to the `mcpServers` object: | |
| 136 | 新增: | |
| 137 | 新增:```json | |
| 138 | 新增:{ | |
| 139 | 新增: "mcpServers": { | |
| 140 | 新增: "chrome-devtools": { | |
| 141 | 新增: "command": "npx", | |
| 142 | 新增: "args": ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"] | |
| 143 | 新增: }, | |
| 144 | 新增: "postgres": { | |
| 145 | 新增: "command": "npx", | |
| 146 | 新增: "args": ["-y", "@modelcontextprotocol/server-postgres", "postgresql://localhost:5432/mydb"] | |
| 147 | 新增: } | |
| 148 | 新增: } | |
| 149 | 新增:} | |
| 150 | 新增:``` | |
| 151 | 新增: | |
| 152 | 新增:## Shared configuration | |
| 153 | 新增: | |
| 154 | 新增:The VM mounts your host's `~/.claude/` directory. Login credentials, skills, user-level `CLAUDE.md`, conversation history, and settings all persist across VMs automatically. | |
| 155 | 新增: | |
| 156 | 新增:On first use, you'll need to authenticate. Credentials are stored in `~/.claude/.credentials.json` and reused by all subsequent VMs. | |
| 157 | 新增: | |
| 158 | 新增:This also means the configuration is shared with any Claude Code installation on the host — though we recommend running Claude exclusively in VMs for security. | |
| 159 | 新增: | |
| 98 | 160 | ## How it works |
| 99 | 161 | |
| 100 | 删除:1. **`claude-vm-setup`** creates a Debian 13 VM with Lima, installs dev tools + Chrome + Claude Code, and stops it as a reusable template | |
| 101 | 删除:2. **`claude-vm [args]`** clones the template, mounts your working directory read-write, runs optional `.claude-vm.runtime.sh`, then launches Claude with full permissions (forwarding any arguments to the `claude` command) | |
| 102 | 删除:3. On exit, the cloned VM is stopped and deleted. The template persists for reuse | |
| 162 | 新增:1. **`agent-vm setup`** creates a Debian 13 VM with Lima, runs `agent-vm.setup.sh` inside it to install dev tools + Chrome + agents, and stops it as a reusable base template | |
| 163 | 新增:2. **`agent-vm claude|opencode|codex [args]`** clones the base template into a persistent per-directory VM, mounts your working directory and `~/.claude/`, runs optional `.agent-vm.runtime.sh`, then launches the agent with full permissions | |
| 164 | 新增:3. The VM persists after exit. Running any agent command or `agent-vm shell` in the same directory reuses the same VM | |
| 165 | 新增:4. Use `agent-vm stop` to stop the VM or `agent-vm destroy` to delete it | |
| 103 | 166 | |
| 104 | 167 | Ports opened inside the VM (e.g. by Docker containers) are automatically forwarded to your host by Lima. |
| 105 | 168 | |
| 106 | 删除:## What's in the VM | |
| 169 | 新增:## Project structure | |
| 170 | 新增: | |
| 171 | 新增:| File | Description | | |
| 172 | 新增:|------|-------------| | |
| 173 | 新增:| `agent-vm.sh` | Main script — source this in your shell config | | |
| 174 | 新增:| `agent-vm.setup.sh` | Package installation script that runs inside the base VM during setup | | |
| 175 | 新增: | |
| 176 | 新增:## What's in the VM by default | |
| 107 | 177 | |
| 108 | 178 | | Category | Packages | |
| 109 | 179 | |----------|----------| |
| 110 | 删除:| Core | git, curl, wget, build-essential, jq | | |
| 180 | 新增:| Core | git, curl, wget, jq, build-essential, unzip, zip | | |
| 111 | 181 | | Python | python3, pip, venv | |
| 112 | 删除:| Node.js | Node.js 22 (via NodeSource) | | |
| 182 | 新增:| Node.js | Node.js 24 LTS (via NodeSource) | | |
| 113 | 183 | | Search | ripgrep, fd-find | |
| 184 | 新增:| Utilities | htop, GitHub CLI (gh) | | |
| 114 | 185 | | Browser | Chromium (headless), xvfb | |
| 115 | 186 | | Containers | Docker Engine, Docker Compose | |
| 116 | 删除:| AI | Claude Code, Chrome DevTools MCP server | | |
| 187 | 新增:| AI | Claude Code, OpenCode, Codex CLI, Chrome DevTools MCP server | | |
| 117 | 188 | |
| 118 | 删除:## Why a VM? | |
| 189 | 新增:## Security model | |
| 119 | 190 | |
| 120 | 删除:Running an AI agent with full permissions is powerful but risky. Here's how the options compare: | |
| 191 | 新增:AI coding agents need full permissions to be useful — they install dependencies, run builds, execute tests, start servers. But running `npm install` or `pip install` means executing arbitrary third-party code on your machine. | |
| 192 | 新增: | |
| 193 | 新增:This is not a theoretical risk. The [Shai-Hulud](https://unit42.paloaltonetworks.com/npm-supply-chain-attack/) worm compromised thousands of npm packages in 2025 by injecting malicious code that runs during `npm install`. It harvested npm tokens, GitHub PATs, SSH keys, and cloud credentials from developers' machines, then used those credentials to spread to other packages the developer maintained. All of this happened silently, in the background, while the legitimate install appeared normal. | |
| 194 | 新增: | |
| 195 | 新增:An AI agent running with `--dangerously-skip-permissions` on your host would give such an attack full access to everything: your SSH keys, your cloud credentials, your browser sessions, your entire filesystem. | |
| 196 | 新增: | |
| 197 | 新增:**agent-vm keeps your credentials on the host and runs all code inside the VM.** The VM only has access to: | |
| 198 | 新增: | |
| 199 | 新增:- Your project directory (read-write mount) | |
| 200 | 新增:- `~/.claude/` for agent credentials and settings | |
| 201 | 新增: | |
| 202 | 新增:It has **no access** to your SSH keys, npm tokens, cloud credentials, git config, browser sessions, or anything else on your host. If a supply chain attack executes inside the VM, it finds nothing to steal (except your source code) and nowhere to spread. | |
| 203 | 新增: | |
| 204 | 新增:Meanwhile, your host machine stays clean. You don't need Node.js, Docker, or any dev tooling installed locally. The only host dependency is Lima. Your SSH keys and signing credentials never enter the VM as we recommend running `git commit` on the host yourself. | |
| 205 | 新增: | |
| 206 | 新增:### Why not Docker? | |
| 121 | 207 | |
| 122 | 208 | | | No sandbox | Docker | VM (agent-vm) | |
| 123 | 209 | |---|---|---|---| |
| @@ -129,13 +215,11 @@ Running an AI agent with full permissions is powerful but risky. Here's how the | ||
| 129 | 215 | | Protection from container escapes | None | None | Yes | |
| 130 | 216 | | Browser / GUI tools | Host only | Complex setup | Built-in (headless Chromium) | |
| 131 | 217 | |
| 132 | 删除:Docker containers share the host kernel, so a motivated agent could exploit kernel vulnerabilities or misconfigurations to escape. A VM runs its own kernel — even if the agent gains root inside the VM, it can't reach the host. | |
| 133 | 删除: | |
| 134 | 删除:A VM also avoids the practical headaches of Docker sandboxing. Docker runs natively inside the VM without Docker-in-Docker hacks or socket mounts. Headless Chromium works out of the box without fiddling with `--no-sandbox` flags or shared memory settings. Lima automatically forwards ports from the VM to your host, so if the agent starts a server on port 3000, it's immediately accessible at `localhost:3000`. The agent gets a normal Linux environment where everything just works. | |
| 218 | 新增:Docker containers share the host kernel. A motivated attacker (or a compromised dependency running inside the container) could exploit kernel vulnerabilities to escape. A VM runs its own kernel — even root access inside the VM can't reach the host. | |
| 135 | 219 | |
| 136 | 删除:Finally, using a VM means you don't need Node.js, npm, Docker, or any other dev tooling installed on your host machine. The only host dependency is Lima. All the tools (and their vulnerabilities) live inside the VM. | |
| 220 | 新增:A VM also avoids the practical headaches of Docker sandboxing. Docker runs natively inside the VM without Docker-in-Docker hacks. Headless Chromium works out of the box. Lima automatically forwards ports to your host. The agent gets a normal Linux environment where everything just works. | |
| 137 | 221 | |
| 138 | 删除:For AI agents running with `--dangerously-skip-permissions`, a VM is the only sandbox that provides meaningful security. | |
| 222 | 新增:This workflow also replaces Docker Desktop on the Mac, which has become more and more bloated over the years. | |
| 139 | 223 | |
| 140 | 224 | ## License |
| 141 | 225 | |
| @@ -0,0 +1,101 @@ | ||
| 1 | 新增:#!/usr/bin/env bash | |
| 2 | 新增:# | |
| 3 | 新增:# agent-vm.setup.sh: Package installation script that runs inside the base VM | |
| 4 | 新增:# Part of https://github.com/sylvinus/agent-vm | |
| 5 | 新增:# | |
| 6 | 新增:# This script is executed inside the VM during "agent-vm setup". | |
| 7 | 新增:# | |
| 8 | 新增: | |
| 9 | 新增:set -euo pipefail | |
| 10 | 新增: | |
| 11 | 新增:export DEBIAN_FRONTEND=noninteractive | |
| 12 | 新增: | |
| 13 | 新增:# Disable needrestart's interactive prompts | |
| 14 | 新增:sudo mkdir -p /etc/needrestart/conf.d | |
| 15 | 新增:echo '$nrconf{restart} = '"'"'a'"'"';' | sudo tee /etc/needrestart/conf.d/no-prompt.conf > /dev/null | |
| 16 | 新增: | |
| 17 | 新增:echo "Installing base packages..." | |
| 18 | 新增:sudo apt-get update | |
| 19 | 新增:sudo apt-get install -y \ | |
| 20 | 新增: git curl jq zsh \ | |
| 21 | 新增: wget build-essential \ | |
| 22 | 新增: python3 python3-pip python3-venv \ | |
| 23 | 新增: ripgrep fd-find htop \ | |
| 24 | 新增: unzip zip \ | |
| 25 | 新增: ca-certificates | |
| 26 | 新增: | |
| 27 | 新增:# Set zsh as default shell | |
| 28 | 新增:sudo chsh -s /usr/bin/zsh "$(whoami)" | |
| 29 | 新增: | |
| 30 | 新增:# Install Docker from official repo (includes docker compose) | |
| 31 | 新增:echo "Installing Docker..." | |
| 32 | 新增:sudo install -m 0755 -d /etc/apt/keyrings | |
| 33 | 新增:sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc | |
| 34 | 新增:sudo chmod a+r /etc/apt/keyrings/docker.asc | |
| 35 | 新增: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 | |
| 36 | 新增:sudo apt-get update | |
| 37 | 新增:sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin | |
| 38 | 新增:sudo usermod -aG docker "$(whoami)" | |
| 39 | 新增: | |
| 40 | 新增:# Install Node.js 24 LTS (needed for MCP servers) | |
| 41 | 新增:echo "Installing Node.js 24..." | |
| 42 | 新增:curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash - | |
| 43 | 新增:sudo apt-get install -y nodejs | |
| 44 | 新增: | |
| 45 | 新增:# Install Chromium and dependencies for headless browsing | |
| 46 | 新增:echo "Installing Chromium..." | |
| 47 | 新增:sudo apt-get install -y chromium fonts-liberation xvfb | |
| 48 | 新增:sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome | |
| 49 | 新增:sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome-stable | |
| 50 | 新增:sudo mkdir -p /opt/google/chrome | |
| 51 | 新增:sudo ln -sf /usr/bin/chromium /opt/google/chrome/chrome | |
| 52 | 新增: | |
| 53 | 新增:# Install GitHub CLI from official repo | |
| 54 | 新增:echo "Installing GitHub CLI..." | |
| 55 | 新增:sudo mkdir -p -m 755 /etc/apt/keyrings | |
| 56 | 新增:wget -qO- https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null | |
| 57 | 新增:sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg | |
| 58 | 新增: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 | |
| 59 | 新增:sudo apt-get update | |
| 60 | 新增:sudo apt-get install -y gh | |
| 61 | 新增: | |
| 62 | 新增:# Install Claude Code | |
| 63 | 新增:echo "Installing Claude Code..." | |
| 64 | 新增:curl -fsSL https://claude.ai/install.sh | bash | |
| 65 | 新增:echo 'export PATH=$HOME/.local/bin:$HOME/.claude/local/bin:$PATH' >> ~/.zshrc | |
| 66 | 新增:echo 'export PS1="vm:%1~%% "' >> ~/.zshrc | |
| 67 | 新增: | |
| 68 | 新增:# Install OpenCode | |
| 69 | 新增:echo "Installing OpenCode..." | |
| 70 | 新增:curl -fsSL https://raw.githubusercontent.com/opencode-ai/opencode/refs/heads/main/install | bash | |
| 71 | 新增:echo 'export PATH=$HOME/.opencode/bin:$PATH' >> ~/.zshrc | |
| 72 | 新增: | |
| 73 | 新增:# Add PATH to .zshenv so non-interactive shells (limactl shell vmname cmd) also find the tools | |
| 74 | 新增:echo 'export PATH=$HOME/.local/bin:$HOME/.claude/local/bin:$HOME/.opencode/bin:$PATH' >> ~/.zshenv | |
| 75 | 新增: | |
| 76 | 新增:# Install Codex CLI | |
| 77 | 新增:echo "Installing Codex CLI..." | |
| 78 | 新增:sudo npm i -g @openai/codex | |
| 79 | 新增: | |
| 80 | 新增:# Configure Chrome DevTools MCP server for Claude | |
| 81 | 新增:echo "Configuring Chrome MCP server..." | |
| 82 | 新增:CONFIG="$HOME/.claude.json" | |
| 83 | 新增:if [ -f "$CONFIG" ]; then | |
| 84 | 新增: jq '.mcpServers["chrome-devtools"] = { | |
| 85 | 新增: "command": "npx", | |
| 86 | 新增: "args": ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"] | |
| 87 | 新增: }' "$CONFIG" > "$CONFIG.tmp" && mv "$CONFIG.tmp" "$CONFIG" | |
| 88 | 新增:else | |
| 89 | 新增: cat > "$CONFIG" << 'JSON' | |
| 90 | 新增:{ | |
| 91 | 新增: "mcpServers": { | |
| 92 | 新增: "chrome-devtools": { | |
| 93 | 新增: "command": "npx", | |
| 94 | 新增: "args": ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"] | |
| 95 | 新增: } | |
| 96 | 新增: } | |
| 97 | 新增:} | |
| 98 | 新增:JSON | |
| 99 | 新增:fi | |
| 100 | 新增: | |
| 101 | 新增:echo "VM setup complete." | |
| @@ -0,0 +1,461 @@ | ||
| 1 | 新增:#!/usr/bin/env bash | |
| 2 | 新增:# | |
| 3 | 新增:# agent-vm: Run AI coding agents inside sandboxed Lima VMs | |
| 4 | 新增:# Part of https://github.com/sylvinus/agent-vm | |
| 5 | 新增:# | |
| 6 | 新增:# Source this file in your shell config: | |
| 7 | 新增:# source /path/to/agent-vm/agent-vm.sh | |
| 8 | 新增:# | |
| 9 | 新增:# Usage: | |
| 10 | 新增:# agent-vm setup - Create the base VM template (run once) | |
| 11 | 新增:# agent-vm claude - Run Claude Code in a persistent VM for cwd | |
| 12 | 新增:# agent-vm opencode - Run OpenCode in a persistent VM for cwd | |
| 13 | 新增:# agent-vm codex - Run Codex CLI in a persistent VM for cwd | |
| 14 | 新增:# agent-vm shell - Open a shell in the persistent VM for cwd | |
| 15 | 新增:# agent-vm stop - Stop the VM for cwd | |
| 16 | 新增:# agent-vm destroy - Stop and delete the VM for cwd | |
| 17 | 新增:# agent-vm list - List all agent-vm VMs | |
| 18 | 新增:# agent-vm status - Show VM status for cwd | |
| 19 | 新增:# agent-vm help - Show help | |
| 20 | 新增:# | |
| 21 | 新增: | |
| 22 | 新增:AGENT_VM_TEMPLATE="agent-vm-base" | |
| 23 | 新增:AGENT_VM_STATE_DIR="${HOME}/.agent-vm" | |
| 24 | 新增:AGENT_VM_CONFIG_DIR="${HOME}/.claude" | |
| 25 | 新增:AGENT_VM_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)" | |
| 26 | 新增: | |
| 27 | 新增:# Generate a deterministic VM name for a directory | |
| 28 | 新增:_agent_vm_name() { | |
| 29 | 新增: local dir="${1:-$(pwd)}" | |
| 30 | 新增: local hash | |
| 31 | 新增: hash=$(echo -n "$dir" | shasum -a 256 | cut -c1-8) | |
| 32 | 新增: local base | |
| 33 | 新增: base=$(basename "$dir" | tr -cs 'a-zA-Z0-9' '-' | sed 's/^-//;s/-$//') | |
| 34 | 新增: echo "agent-vm-${base}-${hash}" | |
| 35 | 新增:} | |
| 36 | 新增: | |
| 37 | 新增:# Check if a VM exists (any state) | |
| 38 | 新增:_agent_vm_exists() { | |
| 39 | 新增: limactl list -q 2>/dev/null | grep -q "^${1}$" | |
| 40 | 新增:} | |
| 41 | 新增: | |
| 42 | 新增:# Check if a VM is running | |
| 43 | 新增:_agent_vm_running() { | |
| 44 | 新增: limactl list --format '{{.Name}} {{.Status}}' 2>/dev/null | grep -q "^${1} Running$" | |
| 45 | 新增:} | |
| 46 | 新增: | |
| 47 | 新增:# Ensure the VM for cwd exists and is running, creating/starting as needed | |
| 48 | 新增:# Usage: _agent_vm_ensure_running <vm_name> <host_dir> [--disk GB] [--memory GB] [--reset] | |
| 49 | 新增:_agent_vm_ensure_running() { | |
| 50 | 新增: local vm_name="$1" | |
| 51 | 新增: local host_dir="$2" | |
| 52 | 新增: shift 2 | |
| 53 | 新增: local disk="" memory="" reset="" | |
| 54 | 新增: while [[ $# -gt 0 ]]; do | |
| 55 | 新增: case "$1" in | |
| 56 | 新增: --disk) disk="$2"; shift 2 ;; | |
| 57 | 新增: --memory) memory="$2"; shift 2 ;; | |
| 58 | 新增: --reset) reset=1; shift ;; | |
| 59 | 新增: *) shift ;; | |
| 60 | 新增: esac | |
| 61 | 新增: done | |
| 62 | 新增: | |
| 63 | 新增: if ! limactl list -q 2>/dev/null | grep -q "^${AGENT_VM_TEMPLATE}$"; then | |
| 64 | 新增: echo "Error: Base VM not found. Run 'agent-vm setup' first." >&2 | |
| 65 | 新增: return 1 | |
| 66 | 新增: fi | |
| 67 | 新增: | |
| 68 | 新增: mkdir -p "$AGENT_VM_CONFIG_DIR" | |
| 69 | 新增: | |
| 70 | 新增: # Destroy existing VM if --reset was requested | |
| 71 | 新增: if [[ -n "$reset" ]] && _agent_vm_exists "$vm_name"; then | |
| 72 | 新增: echo "Resetting VM '$vm_name'..." | |
| 73 | 新增: limactl stop "$vm_name" &>/dev/null | |
| 74 | 新增: limactl delete "$vm_name" --force &>/dev/null | |
| 75 | 新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" | |
| 76 | 新增: fi | |
| 77 | 新增: | |
| 78 | 新增: if ! _agent_vm_exists "$vm_name"; then | |
| 79 | 新增: echo "Creating VM '$vm_name'..." | |
| 80 | 新增: local clone_args=( | |
| 81 | 新增: --set ".mounts=[{\"location\":\"${host_dir}\",\"writable\":true},{\"location\":\"${AGENT_VM_CONFIG_DIR}\",\"mountPoint\":\"/home/${USER}.linux/.claude\",\"writable\":true}]" | |
| 82 | 新增: --tty=false | |
| 83 | 新增: ) | |
| 84 | 新增: [[ -n "$disk" ]] && clone_args+=(--set ".disk=\"${disk}GiB\"") | |
| 85 | 新增: [[ -n "$memory" ]] && clone_args+=(--set ".memory=\"${memory}GiB\"") | |
| 86 | 新增: limactl clone "$AGENT_VM_TEMPLATE" "$vm_name" "${clone_args[@]}" &>/dev/null | |
| 87 | 新增: # Record which base version this VM was cloned from | |
| 88 | 新增: local base_ver="$AGENT_VM_STATE_DIR/.agent-vm-base-version" | |
| 89 | 新增: if [[ -f "$base_ver" ]]; then | |
| 90 | 新增: cp "$base_ver" "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" | |
| 91 | 新增: fi | |
| 92 | 新增: elif [[ -n "$disk" || -n "$memory" ]]; then | |
| 93 | 新增: # Auto-resize existing VM if --disk or --memory changed | |
| 94 | 新增: local edit_args=() | |
| 95 | 新增: [[ -n "$disk" ]] && edit_args+=(--set ".disk=\"${disk}GiB\"") | |
| 96 | 新增: [[ -n "$memory" ]] && edit_args+=(--set ".memory=\"${memory}GiB\"") | |
| 97 | 新增: if _agent_vm_running "$vm_name"; then | |
| 98 | 新增: echo "VM '$vm_name' is currently running. It must be stopped to apply new resource settings." | |
| 99 | 新增: printf "Stop the VM and apply changes? [y/N] " | |
| 100 | 新增: local reply | |
| 101 | 新增: read -r reply | |
| 102 | 新增: if [[ ! "$reply" =~ ^[Yy]$ ]]; then | |
| 103 | 新增: echo "Aborted. Starting with current settings." | |
| 104 | 新增: return 0 | |
| 105 | 新增: fi | |
| 106 | 新增: echo "Stopping VM..." | |
| 107 | 新增: limactl stop "$vm_name" &>/dev/null | |
| 108 | 新增: fi | |
| 109 | 新增: echo "Updating VM resources..." | |
| 110 | 新增: limactl edit "$vm_name" "${edit_args[@]}" &>/dev/null | |
| 111 | 新增: fi | |
| 112 | 新增: | |
| 113 | 新增: # Warn if this VM was cloned from an older base | |
| 114 | 新增: local base_ver="$AGENT_VM_STATE_DIR/.agent-vm-base-version" | |
| 115 | 新增: local vm_ver="$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" | |
| 116 | 新增: if [[ -f "$base_ver" ]] && { [[ ! -f "$vm_ver" ]] || [[ "$(cat "$base_ver")" != "$(cat "$vm_ver")" ]]; }; then | |
| 117 | 新增: echo "Warning: Base VM has been updated since this VM was cloned. Use --reset to re-clone from the new base." >&2 | |
| 118 | 新增: fi | |
| 119 | 新增: | |
| 120 | 新增: if ! _agent_vm_running "$vm_name"; then | |
| 121 | 新增: echo "Starting VM '$vm_name'..." | |
| 122 | 新增: limactl start "$vm_name" &>/dev/null | |
| 123 | 新增: fi | |
| 124 | 新增: | |
| 125 | 新增: # Run project-specific runtime script if it exists | |
| 126 | 新增: if [ -f "${host_dir}/.agent-vm.runtime.sh" ]; then | |
| 127 | 新增: echo "Running project runtime setup..." | |
| 128 | 新增: limactl shell --workdir "$host_dir" "$vm_name" zsh -l < "${host_dir}/.agent-vm.runtime.sh" | |
| 129 | 新增: fi | |
| 130 | 新增:} | |
| 131 | 新增: | |
| 132 | 新增:agent-vm() { | |
| 133 | 新增: local vm_opts=() | |
| 134 | 新增: # Parse global options before the subcommand | |
| 135 | 新增: while [[ $# -gt 0 ]]; do | |
| 136 | 新增: case "$1" in | |
| 137 | 新增: --disk) | |
| 138 | 新增: vm_opts+=(--disk "$2"); shift 2 ;; | |
| 139 | 新增: --disk=*) | |
| 140 | 新增: vm_opts+=(--disk "${1#*=}"); shift ;; | |
| 141 | 新增: --memory) | |
| 142 | 新增: vm_opts+=(--memory "$2"); shift 2 ;; | |
| 143 | 新增: --memory=*) | |
| 144 | 新增: vm_opts+=(--memory "${1#*=}"); shift ;; | |
| 145 | 新增: --reset) | |
| 146 | 新增: vm_opts+=(--reset); shift ;; | |
| 147 | 新增: *) | |
| 148 | 新增: break ;; | |
| 149 | 新增: esac | |
| 150 | 新增: done | |
| 151 | 新增: | |
| 152 | 新增: local cmd="${1:-help}" | |
| 153 | 新增: shift 2>/dev/null || true | |
| 154 | 新增: | |
| 155 | 新增: case "$cmd" in | |
| 156 | 新增: setup) | |
| 157 | 新增: _agent_vm_setup "${vm_opts[@]}" "$@" | |
| 158 | 新增: ;; | |
| 159 | 新增: claude) | |
| 160 | 新增: _agent_vm_claude "${vm_opts[@]}" "$@" | |
| 161 | 新增: ;; | |
| 162 | 新增: opencode) | |
| 163 | 新增: _agent_vm_opencode "${vm_opts[@]}" "$@" | |
| 164 | 新增: ;; | |
| 165 | 新增: codex) | |
| 166 | 新增: _agent_vm_codex "${vm_opts[@]}" "$@" | |
| 167 | 新增: ;; | |
| 168 | 新增: shell) | |
| 169 | 新增: _agent_vm_shell "${vm_opts[@]}" "$@" | |
| 170 | 新增: ;; | |
| 171 | 新增: stop) | |
| 172 | 新增: _agent_vm_stop "$@" | |
| 173 | 新增: ;; | |
| 174 | 新增: destroy) | |
| 175 | 新增: _agent_vm_destroy "$@" | |
| 176 | 新增: ;; | |
| 177 | 新增: list) | |
| 178 | 新增: _agent_vm_list "$@" | |
| 179 | 新增: ;; | |
| 180 | 新增: status) | |
| 181 | 新增: _agent_vm_status "$@" | |
| 182 | 新增: ;; | |
| 183 | 新增: help|--help|-h) | |
| 184 | 新增: _agent_vm_help | |
| 185 | 新增: ;; | |
| 186 | 新增: *) | |
| 187 | 新增: echo "Unknown command: $cmd" >&2 | |
| 188 | 新增: echo "Run 'agent-vm help' for usage." >&2 | |
| 189 | 新增: return 1 | |
| 190 | 新增: ;; | |
| 191 | 新增: esac | |
| 192 | 新增:} | |
| 193 | 新增: | |
| 194 | 新增:_agent_vm_help() { | |
| 195 | 新增: cat << 'EOF' | |
| 196 | 新增:Usage: agent-vm [options] <command> [args] | |
| 197 | 新增: | |
| 198 | 新增:Commands: | |
| 199 | 新增: setup Create the base VM template (run once) | |
| 200 | 新增: claude [args] Run Claude Code in the VM for the current directory | |
| 201 | 新增: opencode [args] Run OpenCode in the VM for the current directory | |
| 202 | 新增: codex [args] Run Codex CLI in the VM for the current directory | |
| 203 | 新增: shell Open a shell in the VM for the current directory | |
| 204 | 新增: stop Stop the VM for the current directory | |
| 205 | 新增: destroy Stop and delete the VM for the current directory | |
| 206 | 新增: list List all agent-vm VMs | |
| 207 | 新增: status Show VM status for the current directory | |
| 208 | 新增: help Show this help | |
| 209 | 新增: | |
| 210 | 新增:VM options (for claude, opencode, codex, shell): | |
| 211 | 新增: --disk GB VM disk size (default: 20) | |
| 212 | 新增: --memory GB VM memory (default: 8) | |
| 213 | 新增: --reset Destroy and re-clone the VM from the base template | |
| 214 | 新增: | |
| 215 | 新增:Examples: | |
| 216 | 新增: agent-vm setup # Create base VM | |
| 217 | 新增: agent-vm claude # Run Claude in a VM | |
| 218 | 新增: agent-vm opencode # Run OpenCode in a VM | |
| 219 | 新增: agent-vm codex # Run Codex in a VM | |
| 220 | 新增: agent-vm --disk 50 --memory 16 claude # With custom resources | |
| 221 | 新增: agent-vm --reset claude # Fresh VM from base template | |
| 222 | 新增: agent-vm shell # Shell into the VM | |
| 223 | 新增: agent-vm claude -p "fix lint errors" # Pass args to claude | |
| 224 | 新增: | |
| 225 | 新增:VMs are persistent and unique per directory. Running "agent-vm shell" or | |
| 226 | 新增:"agent-vm claude" in the same directory will reuse the same VM. | |
| 227 | 新增: | |
| 228 | 新增:Customization: | |
| 229 | 新增: ~/.agent-vm.setup.sh Per-user setup (runs during "agent-vm setup") | |
| 230 | 新增: <project>/.agent-vm.runtime.sh Per-project setup (runs on each VM start) | |
| 231 | 新增: | |
| 232 | 新增:More info: https://github.com/sylvinus/agent-vm | |
| 233 | 新增:EOF | |
| 234 | 新增:} | |
| 235 | 新增: | |
| 236 | 新增:_agent_vm_setup() { | |
| 237 | 新增: local disk=20 | |
| 238 | 新增: local memory=8 | |
| 239 | 新增: | |
| 240 | 新增: while [[ $# -gt 0 ]]; do | |
| 241 | 新增: case "$1" in | |
| 242 | 新增: --help|-h) | |
| 243 | 新增: echo "Usage: agent-vm setup [--disk GB] [--memory GB]" | |
| 244 | 新增: echo "" | |
| 245 | 新增: echo "Create a base VM template with dev tools and agents pre-installed." | |
| 246 | 新增: echo "" | |
| 247 | 新增: echo "Options:" | |
| 248 | 新增: echo " --disk GB VM disk size (default: 20)" | |
| 249 | 新增: echo " --memory GB VM memory (default: 8)" | |
| 250 | 新增: echo " --help Show this help" | |
| 251 | 新增: return 0 | |
| 252 | 新增: ;; | |
| 253 | 新增: --disk) | |
| 254 | 新增: disk="$2" | |
| 255 | 新增: shift 2 | |
| 256 | 新增: ;; | |
| 257 | 新增: --disk=*) | |
| 258 | 新增: disk="${1#*=}" | |
| 259 | 新增: shift | |
| 260 | 新增: ;; | |
| 261 | 新增: --memory) | |
| 262 | 新增: memory="$2" | |
| 263 | 新增: shift 2 | |
| 264 | 新增: ;; | |
| 265 | 新增: --memory=*) | |
| 266 | 新增: memory="${1#*=}" | |
| 267 | 新增: shift | |
| 268 | 新增: ;; | |
| 269 | 新增: *) | |
| 270 | 新增: echo "Unknown option: $1" >&2 | |
| 271 | 新增: echo "Usage: agent-vm setup [--disk GB] [--memory GB]" >&2 | |
| 272 | 新增: return 1 | |
| 273 | 新增: ;; | |
| 274 | 新增: esac | |
| 275 | 新增: done | |
| 276 | 新增: | |
| 277 | 新增: if ! command -v limactl &>/dev/null; then | |
| 278 | 新增: if command -v brew &>/dev/null; then | |
| 279 | 新增: echo "Installing Lima..." | |
| 280 | 新增: brew install lima | |
| 281 | 新增: else | |
| 282 | 新增: echo "Error: Lima is required. Install from https://lima-vm.io/docs/installation/" >&2 | |
| 283 | 新增: return 1 | |
| 284 | 新增: fi | |
| 285 | 新增: fi | |
| 286 | 新增: | |
| 287 | 新增: limactl stop "$AGENT_VM_TEMPLATE" &>/dev/null | |
| 288 | 新增: limactl delete "$AGENT_VM_TEMPLATE" --force &>/dev/null | |
| 289 | 新增: | |
| 290 | 新增: echo "Creating base VM..." | |
| 291 | 新增: limactl create --name="$AGENT_VM_TEMPLATE" template:debian-13 \ | |
| 292 | 新增: --set '.mounts=[]' \ | |
| 293 | 新增: --disk="$disk" \ | |
| 294 | 新增: --memory="$memory" \ | |
| 295 | 新增: --tty=false &>/dev/null || { echo "Error: Failed to create base VM." >&2; return 1; } | |
| 296 | 新增: limactl start "$AGENT_VM_TEMPLATE" &>/dev/null || { echo "Error: Failed to start base VM." >&2; return 1; } | |
| 297 | 新增: | |
| 298 | 新增: # Run the setup script inside the VM | |
| 299 | 新增: echo "Installing packages inside VM..." | |
| 300 | 新增: limactl shell "$AGENT_VM_TEMPLATE" bash -l < "${AGENT_VM_SCRIPT_DIR}/agent-vm.setup.sh" || { echo "Error: Setup script failed." >&2; return 1; } | |
| 301 | 新增: | |
| 302 | 新增: # Run user's custom setup script if it exists | |
| 303 | 新增: local user_setup="$HOME/.agent-vm.setup.sh" | |
| 304 | 新增: if [ -f "$user_setup" ]; then | |
| 305 | 新增: echo "Running custom setup from $user_setup..." | |
| 306 | 新增: limactl shell "$AGENT_VM_TEMPLATE" zsh -l < "$user_setup" || { echo "Error: Custom setup script failed." >&2; return 1; } | |
| 307 | 新增: fi | |
| 308 | 新增: | |
| 309 | 新增: limactl stop "$AGENT_VM_TEMPLATE" &>/dev/null | |
| 310 | 新增: | |
| 311 | 新增: mkdir -p "$AGENT_VM_CONFIG_DIR" | |
| 312 | 新增: | |
| 313 | 新增: # Record base VM version so we can warn about stale clones | |
| 314 | 新增: mkdir -p "$AGENT_VM_STATE_DIR" | |
| 315 | 新增: date +%s > "$AGENT_VM_STATE_DIR/.agent-vm-base-version" | |
| 316 | 新增: | |
| 317 | 新增: echo "" | |
| 318 | 新增: echo "Base VM ready. Run 'agent-vm shell', 'agent-vm claude', 'agent-vm opencode', or 'agent-vm codex' in any project directory." | |
| 319 | 新增: echo "Note: Existing VMs were not updated. Use --reset to re-clone them from the new base." | |
| 320 | 新增:} | |
| 321 | 新增: | |
| 322 | 新增:_agent_vm_claude() { | |
| 323 | 新增: local vm_opts=() | |
| 324 | 新增: local args=() | |
| 325 | 新增: while [[ $# -gt 0 ]]; do | |
| 326 | 新增: case "$1" in | |
| 327 | 新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;; | |
| 328 | 新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;; | |
| 329 | 新增: --reset) vm_opts+=(--reset); shift ;; | |
| 330 | 新增: *) args+=("$1"); shift ;; | |
| 331 | 新增: esac | |
| 332 | 新增: done | |
| 333 | 新增: local host_dir | |
| 334 | 新增: host_dir="$(pwd)" | |
| 335 | 新增: local vm_name | |
| 336 | 新增: vm_name="$(_agent_vm_name "$host_dir")" | |
| 337 | 新增: | |
| 338 | 新增: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 | |
| 339 | 新增: | |
| 340 | 新增: limactl shell --workdir "$host_dir" "$vm_name" claude --dangerously-skip-permissions "${args[@]}" | |
| 341 | 新增:} | |
| 342 | 新增: | |
| 343 | 新增:_agent_vm_opencode() { | |
| 344 | 新增: local vm_opts=() | |
| 345 | 新增: local args=() | |
| 346 | 新增: while [[ $# -gt 0 ]]; do | |
| 347 | 新增: case "$1" in | |
| 348 | 新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;; | |
| 349 | 新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;; | |
| 350 | 新增: --reset) vm_opts+=(--reset); shift ;; | |
| 351 | 新增: *) args+=("$1"); shift ;; | |
| 352 | 新增: esac | |
| 353 | 新增: done | |
| 354 | 新增: local host_dir | |
| 355 | 新增: host_dir="$(pwd)" | |
| 356 | 新增: local vm_name | |
| 357 | 新增: vm_name="$(_agent_vm_name "$host_dir")" | |
| 358 | 新增: | |
| 359 | 新增: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 | |
| 360 | 新增: | |
| 361 | 新增: limactl shell --workdir "$host_dir" "$vm_name" opencode --dangerously-skip-permissions "${args[@]}" | |
| 362 | 新增:} | |
| 363 | 新增: | |
| 364 | 新增:_agent_vm_codex() { | |
| 365 | 新增: local vm_opts=() | |
| 366 | 新增: local args=() | |
| 367 | 新增: while [[ $# -gt 0 ]]; do | |
| 368 | 新增: case "$1" in | |
| 369 | 新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;; | |
| 370 | 新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;; | |
| 371 | 新增: --reset) vm_opts+=(--reset); shift ;; | |
| 372 | 新增: *) args+=("$1"); shift ;; | |
| 373 | 新增: esac | |
| 374 | 新增: done | |
| 375 | 新增: local host_dir | |
| 376 | 新增: host_dir="$(pwd)" | |
| 377 | 新增: local vm_name | |
| 378 | 新增: vm_name="$(_agent_vm_name "$host_dir")" | |
| 379 | 新增: | |
| 380 | 新增: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 | |
| 381 | 新增: | |
| 382 | 新增: limactl shell --workdir "$host_dir" "$vm_name" codex --full-auto "${args[@]}" | |
| 383 | 新增:} | |
| 384 | 新增: | |
| 385 | 新增:_agent_vm_shell() { | |
| 386 | 新增: local vm_opts=() | |
| 387 | 新增: while [[ $# -gt 0 ]]; do | |
| 388 | 新增: case "$1" in | |
| 389 | 新增: --disk) vm_opts+=(--disk "$2"); shift 2 ;; | |
| 390 | 新增: --memory) vm_opts+=(--memory "$2"); shift 2 ;; | |
| 391 | 新增: --reset) vm_opts+=(--reset); shift ;; | |
| 392 | 新增: *) shift ;; | |
| 393 | 新增: esac | |
| 394 | 新增: done | |
| 395 | 新增: local host_dir | |
| 396 | 新增: host_dir="$(pwd)" | |
| 397 | 新增: local vm_name | |
| 398 | 新增: vm_name="$(_agent_vm_name "$host_dir")" | |
| 399 | 新增: | |
| 400 | 新增: _agent_vm_ensure_running "$vm_name" "$host_dir" "${vm_opts[@]}" || return 1 | |
| 401 | 新增: | |
| 402 | 新增: echo "VM: $vm_name | Dir: $host_dir" | |
| 403 | 新增: echo "Type 'exit' to leave (VM keeps running). Use 'agent-vm stop' to stop it." | |
| 404 | 新增: limactl shell --workdir "$host_dir" "$vm_name" zsh -l | |
| 405 | 新增:} | |
| 406 | 新增: | |
| 407 | 新增:_agent_vm_stop() { | |
| 408 | 新增: local host_dir | |
| 409 | 新增: host_dir="$(pwd)" | |
| 410 | 新增: local vm_name | |
| 411 | 新增: vm_name="$(_agent_vm_name "$host_dir")" | |
| 412 | 新增: | |
| 413 | 新增: if ! _agent_vm_exists "$vm_name"; then | |
| 414 | 新增: echo "No VM found for this directory." >&2 | |
| 415 | 新增: return 1 | |
| 416 | 新增: fi | |
| 417 | 新增: | |
| 418 | 新增: echo "Stopping VM '$vm_name'..." | |
| 419 | 新增: limactl stop "$vm_name" &>/dev/null | |
| 420 | 新增: echo "VM stopped." | |
| 421 | 新增:} | |
| 422 | 新增: | |
| 423 | 新增:_agent_vm_destroy() { | |
| 424 | 新增: local host_dir | |
| 425 | 新增: host_dir="$(pwd)" | |
| 426 | 新增: local vm_name | |
| 427 | 新增: vm_name="$(_agent_vm_name "$host_dir")" | |
| 428 | 新增: | |
| 429 | 新增: if ! _agent_vm_exists "$vm_name"; then | |
| 430 | 新增: echo "No VM found for this directory." >&2 | |
| 431 | 新增: return 1 | |
| 432 | 新增: fi | |
| 433 | 新增: | |
| 434 | 新增: echo "Stopping and deleting VM '$vm_name'..." | |
| 435 | 新增: limactl stop "$vm_name" &>/dev/null | |
| 436 | 新增: limactl delete "$vm_name" --force &>/dev/null | |
| 437 | 新增: rm -f "$AGENT_VM_STATE_DIR/.agent-vm-version-${vm_name}" | |
| 438 | 新增: echo "VM destroyed." | |
| 439 | 新增:} | |
| 440 | 新增: | |
| 441 | 新增:_agent_vm_list() { | |
| 442 | 新增: limactl list | head -1 | |
| 443 | 新增: limactl list | grep "^agent-vm-" || echo "(no VMs)" | |
| 444 | 新增:} | |
| 445 | 新增: | |
| 446 | 新增:_agent_vm_status() { | |
| 447 | 新增: local host_dir | |
| 448 | 新增: host_dir="$(pwd)" | |
| 449 | 新增: local vm_name | |
| 450 | 新增: vm_name="$(_agent_vm_name "$host_dir")" | |
| 451 | 新增: | |
| 452 | 新增: if ! _agent_vm_exists "$vm_name"; then | |
| 453 | 新增: echo "No VM for this directory." | |
| 454 | 新增: echo "VM name: $vm_name" | |
| 455 | 新增: return 0 | |
| 456 | 新增: fi | |
| 457 | 新增: | |
| 458 | 新增: echo "VM name: $vm_name" | |
| 459 | 新增: limactl list | head -1 | |
| 460 | 新增: limactl list | grep "$vm_name" | |
| 461 | 新增:} | |
| @@ -1,247 +0,0 @@ | ||
| 1 | 删除:#!/usr/bin/env bash | |
| 2 | 删除:# | |
| 3 | 删除:# agent-vm / claude-vm: Run Claude Code inside a sandboxed Lima VM | |
| 4 | 删除:# Part of https://github.com/sylvinus/agent-vm | |
| 5 | 删除:# | |
| 6 | 删除:# Source this file in your shell config: | |
| 7 | 删除:# source /path/to/agent-vm/claude-vm.sh | |
| 8 | 删除:# | |
| 9 | 删除:# Functions: | |
| 10 | 删除:# claude-vm-setup - Create the VM template (run once) | |
| 11 | 删除:# claude-vm [args] - Run Claude in a fresh VM with cwd mounted (args forwarded to claude) | |
| 12 | 删除:# claude-vm-shell - Open a debug shell in a fresh VM | |
| 13 | 删除: | |
| 14 | 删除:CLAUDE_VM_TEMPLATE="claude-template" | |
| 15 | 删除: | |
| 16 | 删除:claude-vm-setup() { | |
| 17 | 删除: local minimal=false | |
| 18 | 删除: local disk=20 | |
| 19 | 删除: local memory=8 | |
| 20 | 删除: | |
| 21 | 删除: while [[ $# -gt 0 ]]; do | |
| 22 | 删除: case "$1" in | |
| 23 | 删除: --help|-h) | |
| 24 | 删除: echo "Usage: claude-vm-setup [--minimal] [--disk GB] [--memory GB]" | |
| 25 | 删除: echo "" | |
| 26 | 删除: echo "Create a VM template with Claude Code pre-installed." | |
| 27 | 删除: echo "" | |
| 28 | 删除: echo "Options:" | |
| 29 | 删除: echo " --minimal Only install git, curl, jq, and Claude Code" | |
| 30 | 删除: echo " --disk GB VM disk size (default: 20)" | |
| 31 | 删除: echo " --memory GB VM memory (default: 8)" | |
| 32 | 删除: echo " --help Show this help" | |
| 33 | 删除: return 0 | |
| 34 | 删除: ;; | |
| 35 | 删除: --minimal) | |
| 36 | 删除: minimal=true | |
| 37 | 删除: shift | |
| 38 | 删除: ;; | |
| 39 | 删除: --disk) | |
| 40 | 删除: disk="$2" | |
| 41 | 删除: shift 2 | |
| 42 | 删除: ;; | |
| 43 | 删除: --disk=*) | |
| 44 | 删除: disk="${1#*=}" | |
| 45 | 删除: shift | |
| 46 | 删除: ;; | |
| 47 | 删除: --memory) | |
| 48 | 删除: memory="$2" | |
| 49 | 删除: shift 2 | |
| 50 | 删除: ;; | |
| 51 | 删除: --memory=*) | |
| 52 | 删除: memory="${1#*=}" | |
| 53 | 删除: shift | |
| 54 | 删除: ;; | |
| 55 | 删除: *) | |
| 56 | 删除: echo "Unknown option: $1" >&2 | |
| 57 | 删除: echo "Usage: claude-vm-setup [--minimal] [--disk GB] [--memory GB]" >&2 | |
| 58 | 删除: return 1 | |
| 59 | 删除: ;; | |
| 60 | 删除: esac | |
| 61 | 删除: done | |
| 62 | 删除: | |
| 63 | 删除: if ! command -v limactl &>/dev/null; then | |
| 64 | 删除: if command -v brew &>/dev/null; then | |
| 65 | 删除: echo "Installing Lima..." | |
| 66 | 删除: brew install lima | |
| 67 | 删除: else | |
| 68 | 删除: echo "Error: Lima is required. Install from https://lima-vm.io/docs/installation/" >&2 | |
| 69 | 删除: return 1 | |
| 70 | 删除: fi | |
| 71 | 删除: fi | |
| 72 | 删除: | |
| 73 | 删除: limactl stop "$CLAUDE_VM_TEMPLATE" &>/dev/null | |
| 74 | 删除: limactl delete "$CLAUDE_VM_TEMPLATE" --force &>/dev/null | |
| 75 | 删除: | |
| 76 | 删除: echo "Creating VM template..." | |
| 77 | 删除: limactl create --name="$CLAUDE_VM_TEMPLATE" template:debian-13 \ | |
| 78 | 删除: --set '.mounts=[]' \ | |
| 79 | 删除: --disk="$disk" \ | |
| 80 | 删除: --memory="$memory" \ | |
| 81 | 删除: --tty=false | |
| 82 | 删除: limactl start "$CLAUDE_VM_TEMPLATE" | |
| 83 | 删除: | |
| 84 | 删除: # Disable needrestart's interactive prompts | |
| 85 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo bash -c 'mkdir -p /etc/needrestart/conf.d && echo "\$nrconf{restart} = '"'"'a'"'"';" > /etc/needrestart/conf.d/no-prompt.conf' | |
| 86 | 删除: | |
| 87 | 删除: echo "Installing base packages..." | |
| 88 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo DEBIAN_FRONTEND=noninteractive apt-get update | |
| 89 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \ | |
| 90 | 删除: git curl jq | |
| 91 | 删除: | |
| 92 | 删除: if ! $minimal; then | |
| 93 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \ | |
| 94 | 删除: wget build-essential \ | |
| 95 | 删除: python3 python3-pip python3-venv \ | |
| 96 | 删除: ripgrep fd-find htop \ | |
| 97 | 删除: unzip zip \ | |
| 98 | 删除: ca-certificates | |
| 99 | 删除: | |
| 100 | 删除: # Install Docker from official repo (includes docker compose) | |
| 101 | 删除: echo "Installing Docker..." | |
| 102 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash -c ' | |
| 103 | 删除: sudo install -m 0755 -d /etc/apt/keyrings | |
| 104 | 删除: sudo curl -fsSL https://download.docker.com/linux/debian/gpg -o /etc/apt/keyrings/docker.asc | |
| 105 | 删除: sudo chmod a+r /etc/apt/keyrings/docker.asc | |
| 106 | 删除: 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 | |
| 107 | 删除: ' | |
| 108 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo DEBIAN_FRONTEND=noninteractive apt-get update | |
| 109 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \ | |
| 110 | 删除: docker-ce docker-ce-cli containerd.io docker-compose-plugin | |
| 111 | 删除: | |
| 112 | 删除: # Add user to docker group | |
| 113 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash -c 'sudo usermod -aG docker $(whoami)' | |
| 114 | 删除: | |
| 115 | 删除: # Install Node.js 22 (needed for MCP servers) | |
| 116 | 删除: echo "Installing Node.js 22..." | |
| 117 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash -c "curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -" | |
| 118 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo DEBIAN_FRONTEND=noninteractive apt-get install -y nodejs | |
| 119 | 删除: | |
| 120 | 删除: # Install Chromium and dependencies for headless browsing | |
| 121 | 删除: echo "Installing Chromium..." | |
| 122 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo DEBIAN_FRONTEND=noninteractive apt-get install -y \ | |
| 123 | 删除: chromium \ | |
| 124 | 删除: fonts-liberation \ | |
| 125 | 删除: xvfb | |
| 126 | 删除: # Symlink so tools looking for google-chrome find Chromium | |
| 127 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome | |
| 128 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" sudo ln -sf /usr/bin/chromium /usr/bin/google-chrome-stable | |
| 129 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash -c 'sudo mkdir -p /opt/google/chrome && sudo ln -sf /usr/bin/chromium /opt/google/chrome/chrome' | |
| 130 | 删除: fi | |
| 131 | 删除: | |
| 132 | 删除: # Install Claude Code | |
| 133 | 删除: echo "Installing Claude Code..." | |
| 134 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash -c "curl -fsSL https://claude.ai/install.sh | bash" | |
| 135 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash -c 'echo "export PATH=\$HOME/.local/bin:\$HOME/.claude/local/bin:\$PATH" >> ~/.bashrc' | |
| 136 | 删除: | |
| 137 | 删除: # Authenticate Claude (saves token in template, inherited by clones) | |
| 138 | 删除: echo "Setting up Claude authentication..." | |
| 139 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash -lc "claude 'Ok I am logged in, I can exit now.'" | |
| 140 | 删除: | |
| 141 | 删除: if ! $minimal; then | |
| 142 | 删除: # Configure Chrome DevTools MCP server for Claude | |
| 143 | 删除: echo "Configuring Chrome MCP server..." | |
| 144 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash << 'VMEOF' | |
| 145 | 删除:CONFIG="$HOME/.claude.json" | |
| 146 | 删除:if [ -f "$CONFIG" ]; then | |
| 147 | 删除: jq '.mcpServers["chrome-devtools"] = { | |
| 148 | 删除: "command": "npx", | |
| 149 | 删除: "args": ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"] | |
| 150 | 删除: }' "$CONFIG" > "$CONFIG.tmp" && mv "$CONFIG.tmp" "$CONFIG" | |
| 151 | 删除:else | |
| 152 | 删除: cat > "$CONFIG" << 'JSON' | |
| 153 | 删除:{ | |
| 154 | 删除: "mcpServers": { | |
| 155 | 删除: "chrome-devtools": { | |
| 156 | 删除: "command": "npx", | |
| 157 | 删除: "args": ["-y", "chrome-devtools-mcp@latest", "--headless=true", "--isolated=true"] | |
| 158 | 删除: } | |
| 159 | 删除: } | |
| 160 | 删除:} | |
| 161 | 删除:JSON | |
| 162 | 删除:fi | |
| 163 | 删除:VMEOF | |
| 164 | 删除: fi | |
| 165 | 删除: | |
| 166 | 删除: # Run user's custom setup script if it exists | |
| 167 | 删除: local user_setup="$HOME/.claude-vm.setup.sh" | |
| 168 | 删除: if [ -f "$user_setup" ]; then | |
| 169 | 删除: echo "Running custom setup from $user_setup..." | |
| 170 | 删除: limactl shell "$CLAUDE_VM_TEMPLATE" bash < "$user_setup" | |
| 171 | 删除: fi | |
| 172 | 删除: | |
| 173 | 删除: limactl stop "$CLAUDE_VM_TEMPLATE" | |
| 174 | 删除: | |
| 175 | 删除: echo "Template ready. Run 'claude-vm' in any project directory." | |
| 176 | 删除:} | |
| 177 | 删除: | |
| 178 | 删除:claude-vm() { | |
| 179 | 删除: local args=("$@") | |
| 180 | 删除: local vm_name="claude-$(basename "$(pwd)" | tr -cs 'a-zA-Z0-9' '-' | sed 's/^-//;s/-$//')-$$" | |
| 181 | 删除: local host_dir="$(pwd)" | |
| 182 | 删除: | |
| 183 | 删除: if ! limactl list -q 2>/dev/null | grep -q "^${CLAUDE_VM_TEMPLATE}$"; then | |
| 184 | 删除: echo "Error: Template VM not found. Run 'claude-vm-setup' first." >&2 | |
| 185 | 删除: return 1 | |
| 186 | 删除: fi | |
| 187 | 删除: | |
| 188 | 删除: _claude_vm_cleanup() { | |
| 189 | 删除: echo "Cleaning up VM..." | |
| 190 | 删除: limactl stop "$vm_name" &>/dev/null | |
| 191 | 删除: limactl delete "$vm_name" --force &>/dev/null | |
| 192 | 删除: } | |
| 193 | 删除: trap _claude_vm_cleanup EXIT INT TERM | |
| 194 | 删除: | |
| 195 | 删除: echo "Starting VM '$vm_name'..." | |
| 196 | 删除: limactl clone "$CLAUDE_VM_TEMPLATE" "$vm_name" \ | |
| 197 | 删除: --set ".mounts=[{\"location\":\"${host_dir}\",\"writable\":true}]" \ | |
| 198 | 删除: --tty=false &>/dev/null | |
| 199 | 删除: | |
| 200 | 删除: limactl start "$vm_name" &>/dev/null | |
| 201 | 删除: | |
| 202 | 删除: # Run project-specific runtime script if it exists | |
| 203 | 删除: if [ -f "${host_dir}/.claude-vm.runtime.sh" ]; then | |
| 204 | 删除: echo "Running project runtime setup..." | |
| 205 | 删除: limactl shell --workdir "$host_dir" "$vm_name" bash -l < "${host_dir}/.claude-vm.runtime.sh" | |
| 206 | 删除: fi | |
| 207 | 删除: | |
| 208 | 删除: limactl shell --workdir "$host_dir" "$vm_name" claude --dangerously-skip-permissions "${args[@]}" | |
| 209 | 删除: | |
| 210 | 删除: _claude_vm_cleanup | |
| 211 | 删除: trap - EXIT INT TERM | |
| 212 | 删除:} | |
| 213 | 删除: | |
| 214 | 删除:claude-vm-shell() { | |
| 215 | 删除: local vm_name="claude-debug-$$" | |
| 216 | 删除: local host_dir="$(pwd)" | |
| 217 | 删除: | |
| 218 | 删除: if ! limactl list -q 2>/dev/null | grep -q "^${CLAUDE_VM_TEMPLATE}$"; then | |
| 219 | 删除: echo "Error: Template VM not found. Run 'claude-vm-setup' first." >&2 | |
| 220 | 删除: return 1 | |
| 221 | 删除: fi | |
| 222 | 删除: | |
| 223 | 删除: _claude_vm_shell_cleanup() { | |
| 224 | 删除: echo "Cleaning up VM..." | |
| 225 | 删除: limactl stop "$vm_name" &>/dev/null | |
| 226 | 删除: limactl delete "$vm_name" --force &>/dev/null | |
| 227 | 删除: } | |
| 228 | 删除: trap _claude_vm_shell_cleanup EXIT INT TERM | |
| 229 | 删除: | |
| 230 | 删除: limactl clone "$CLAUDE_VM_TEMPLATE" "$vm_name" \ | |
| 231 | 删除: --set ".mounts=[{\"location\":\"${host_dir}\",\"writable\":true}]" \ | |
| 232 | 删除: --tty=false &>/dev/null | |
| 233 | 删除: | |
| 234 | 删除: limactl start "$vm_name" &>/dev/null | |
| 235 | 删除: | |
| 236 | 删除: # Run project-specific runtime script if it exists | |
| 237 | 删除: if [ -f "${host_dir}/.claude-vm.runtime.sh" ]; then | |
| 238 | 删除: limactl shell --workdir "$host_dir" "$vm_name" bash -l < "${host_dir}/.claude-vm.runtime.sh" | |
| 239 | 删除: fi | |
| 240 | 删除: | |
| 241 | 删除: echo "VM: $vm_name | Dir: $host_dir" | |
| 242 | 删除: echo "Type 'exit' to stop and delete the VM" | |
| 243 | 删除: limactl shell --workdir "$host_dir" "$vm_name" bash -l | |
| 244 | 删除: | |
| 245 | 删除: _claude_vm_shell_cleanup | |
| 246 | 删除: trap - EXIT INT TERM | |
| 247 | 删除:} | |

