Skip to main content
Glama
README.md
<h1 align="center">winbench</h1>

<p align="center">
  <b>Let Claude Code (or any MCP client) test your Windows program in a real Windows 11 VM.</b><br>
  It ships the build in, runs it, reads and clicks the GUI as text, catches crashes, debugs with cdb,
  and rolls the VM back to a clean snapshot. Free and open source.
</p>

<p align="center">
  <a href="#what-it-does">What it does</a>
  &nbsp;·&nbsp;
  <a href="#tools">Tools</a>
  &nbsp;·&nbsp;
  <a href="#getting-started">Getting started</a>
  &nbsp;·&nbsp;
  <a href="#several-sessions-one-vm">Several sessions, one VM</a>
  &nbsp;·&nbsp;
  <a href="#security-model">Security model</a>
</p>

> [!IMPORTANT]
> The model is an administrator inside the VM by design. Use a VM you can throw away, keep it fenced off
> your other machines (see [Security model](#security-model)), and test only software you are allowed to run.

## What it does

You work on a Windows program from your Mac (or Linux box) with an AI coding agent. winbench gives that
agent a Windows 11 VM on your Proxmox VE server and 45 [MCP](https://modelcontextprotocol.io) tools to
use it, so a session can get from "I changed the code" to "the fix works in the real GUI" on its own:

```
upload(build.exe) -> start -> wait_for(window) -> screen_describe -> ui_click / type_text
-> screen_describe -> crashes -> analyze_dump -> dbg_open / dbg_cmd -> vm_rollback("clean")
```

- **The screen as text.** `screen_describe` turns the window into lines the model can act on, with an id,
  a position and the control's state. No vision model is needed, and when the model can see images,
  screenshots can carry the same ids.
- **Crashes and debugging built in.** Every crash leaves a full dump; `analyze_dump` runs `cdb !analyze -v`
  on it, and `dbg_open` / `dbg_cmd` drive a live cdb session (breakpoints, stacks, registers).
- **Back to clean in about a minute.** `vm_rollback("clean")` restores the VM snapshot and waits for the
  agent to come back.
- **Shared safely.** Several Claude Code or pi sessions can use the same VM; a lease on the host makes them
  take turns ([details](#several-sessions-one-vm)).

What a model sees from `screen_describe(interactive_only=true)` on a Notepad save prompt:

```
Interactive elements of "*notes - Notepad" (hwnd 0x630444):
(modal dialog "Notepad" is open: only its elements are listed)
e1 | Button "Save" | in "Notepad" | id=PrimaryButton | (287,351) 85x32 | focused
e2 | Button "Don't save" | in "Notepad" | id=SecondaryButton | (382,351) 89x32
e3 | Button "Cancel" | in "Notepad" | id=CloseButton | (475,351) 80x32
```

and from the full tree of an app's toolbar:

```
e18 | Button "Brush" | (65,212) 64x32 | checked
e19 | Button "Eraser" | (133,212) 67x32 | unchecked
e22 | Slider "Size" | (377,211) 129x17 | value="24 px" | range=2..200
e24 | Button "Undo" | (550,212) 63x32 | disabled
e46 | MenuItem "System" | (20,20) 22x22 | collapsed
```

## Tools

| Group | Tools |
|---|---|
| See the screen | `screen_describe`, `screen_ocr`, `screenshot`, `list_windows`, `wait_for` |
| Act on it | `ui_click`, `ui_invoke`, `ui_set_value`, `ui_scroll`, `focus_window`, `move_window`, `close_window`, `mouse`, `keys`, `type_text` |
| Files | `upload`, `download`, `ls`, `read_text`, `write_text`, `delete` |
| Processes | `run`, `start`, `processes`, `kill`, `modules` |
| Debugging | `crashes`, `analyze_dump`, `event_log`, `dbg_open`, `dbg_cmd`, `dbg_close` |
| The VM | `vm_status`, `vm_start`, `vm_reset`, `vm_snapshot`, `vm_snapshots`, `vm_delsnapshot`, `vm_rollback`, `vm_screenshot_host`, `vm_keys_host`, `agent_restart` |
| Sharing | `vm_lock`, `vm_unlock`, `vm_lock_status` |

### Reading the UI

- `screen_describe` reads the foreground window (or any window by title or hwnd) through UI Automation.
  Each parent's children come back with all their properties in one cached call, so a 49-element window
  is described in about a quarter of a second.
- Every line shows the state a tester checks: `checked` / `unchecked` / `mixed`, `expanded` / `collapsed`,
  `selected`, `disabled`, `focused`, `readonly`, `password` (the value is never shown), `range=min..max`,
  `scroll v=40%`, `modal` and keyboard shortcuts (`key=Ctrl+S`).
- `interactive_only=true` lists only what you can act on, flat, each with the group it sits in. While a
  modal dialog is open it lists only the dialog, which is all a user could reach.
- `screen_ocr` reads text from custom-drawn UI (games, canvases, owner-drawn controls) with the Windows OCR
  engine.
- `screenshot(annotate=true)` draws the ids of the last describe onto the image.

### Acting on it

- `ui_click` clicks an element by id at its live position; `ui_invoke` presses, toggles, expands or selects
  it without moving the mouse; `ui_set_value` fills an edit box.
- `ui_scroll` scrolls the list, tree or document that contains an element, by page, by line, to a
  percentage, or until the element itself is in view.
- `focus_window` and every element action bring the window to the front. When Windows' focus-stealing
  rule blocks that, winbench forces it by attaching to the foreground window's input queue.
- `move_window` moves, resizes, maximizes or minimizes a window, for layout and minimum-size tests.
- `type_text` pastes exact Unicode text through the clipboard, or types it key by key for controls that
  ignore paste.
- An id from before the screen changed is refused as stale instead of clicking whatever is there now.

## How it fits together

```
Claude Code / pi (your computer)
  |  stdio, one MCP server process per session (server/)
  |  ssh with one restricted key, nothing else:
  |    - tunnel 127.0.0.1:18765 -> <vm-ip>:8765     (agent calls)
  |    - forced command on the host                  (VM power, snapshots, lease)
  v
Proxmox VE host: winbench-gate (host/winbench_gate.py), whitelisted operations on one VM
  v
Windows 11 VM: winbench-agent (agent/), Python 3.12 in the logged-on admin session, elevated,
               HTTP JSON API with a bearer token, supervised by a Scheduled Task
```

- **`agent/`** runs inside the VM. One worker thread owns UI Automation, input and screenshots; a call that
  hangs gets the thread replaced instead of blocking every later call. The Scheduled Task starts it at
  logon and checks every minute, so a crashed or killed agent is back within a minute.
- **`host/winbench_gate.py`** is the only program the restricted key can run on the Proxmox host. It accepts
  `status`, `start`, `stop`, `reset`, `snapshot`, `rollback`, `screenshot`, `sendkey`, `lease` and a few more,
  for one VM id, and logs every call. When the agent is dead it restarts it through the QEMU guest agent.
- **`server/`** is the MCP server your client starts. It turns every failure into a tool error with the
  reason, restarts a dead agent by itself, and tells the model when its call did not run.

## Getting started

### What you need

- A **Proxmox VE** host you have root ssh access to, with the VM disks on ZFS (snapshots and rollback).
- A **Windows 11 VM** on it with the QEMU guest agent installed, a local administrator account named
  `admin` that logs on automatically, and a fixed IPv4 address on a bridge of the host. Turn off Microsoft
  Defender in the VM if your builds are unsigned: it quarantines them, and `winbench.doctor` warns while it
  runs.
- On your computer: macOS or Linux with `bash`, `ssh`, `openssl`, `zip` and [uv](https://docs.astral.sh/uv/),
  plus [Claude Code](https://claude.com/claude-code) and/or [pi](https://github.com/badlogic/pi-mono).

### Install with an AI agent

Let your coding agent do the install. Paste this into Claude Code, Codex, pi or another agent that can
run shell commands on your computer:

```text
Clone https://github.com/007hacky007/winbench and install it for me by following
docs/INSTALL-AGENT.md in that repository. Ask me for every value you cannot discover,
and stop for my confirmation before each change to the Proxmox host or the VM.
```

The [runbook](docs/INSTALL-AGENT.md) tells the agent which values to ask for, how to discover them with
read-only commands (`qm list`, `qm config`, the guest agent), where to pause for your OK and how to
verify the result. You need the prerequisites below and working root ssh to the Proxmox host first.

### Install

1. **Configure.** Copy `winbench.conf.example` to `winbench.conf` and fill in your Proxmox host, VM id and
   VM address. `winbench.conf` is git-ignored; every script and the MCP server read it.

   ```bash
   cp winbench.conf.example winbench.conf
   $EDITOR winbench.conf
   ```

2. **Your computer:** `scripts/install-mac.sh` creates the restricted gate key and the agent token
   (`~/.config/winbench/token`), builds the MCP server's virtualenv and registers `winbench` with Claude Code
   (user scope) and pi. Skip either with `--no-claude` / `--no-pi`.

3. **The host:** `scripts/install-host.sh` installs the gate and adds the restricted key to root's
   `authorized_keys`, limited to the gate and one port forward. Check it:
   `scripts/gate.sh status` must work, and `scripts/gate.sh "status; id"` must print usage and exit 2.

4. **The VM:** `scripts/deploy-vm.sh` copies the agent, the token and `agent/install/setup-vm.ps1` into the
   VM through the QEMU guest agent and runs the setup: Python 3.12, the VC++ runtime, Sysinternals, full
   crash dumps, power settings, the firewall rule and the Scheduled Task. Then install the debugger from
   inside the admin session:

   ```bash
   RPC_VIA=host scripts/rpc.sh run '{"cmd":"winget install --id Microsoft.WinDbg -e --accept-package-agreements --accept-source-agreements --silent","timeout":600}'
   ```

5. **Fence the VM** (recommended): `scripts/install-host.sh --fence` drops all traffic between the VM's
   bridge port and every other guest on the host, IPv6 and ARP included. The VM keeps internet access.

6. **Snapshot it:** `scripts/gate.sh snapshot clean`.

Check every link of the chain with `server/.venv/bin/python -m winbench.doctor`.

### Settings

| Setting (`winbench.conf` or environment) | What it is |
|---|---|
| `WINBENCH_PVE_HOST` | ssh destination of the Proxmox host, e.g. `root@pve.example.lan` |
| `WINBENCH_VMID` | Proxmox id of the Windows VM |
| `WINBENCH_VM_IP`, `WINBENCH_VM_NET`, `WINBENCH_VM_BRIDGE` | the VM's address, its network and bridge (firewall rule and fence) |
| `WINBENCH_GATE_KEY` | the restricted ssh key (default `~/.ssh/winbench_gate`) |
| `WINBENCH_GATE_BIN` | where the gate is installed on the host (default `/usr/local/sbin/winbench-gate`) |
| `WINBENCH_TUNNEL_PORT` | local end of the tunnel (default 18765) |
| `WINBENCH_CONFIG` | path of the config file, if not `./winbench.conf` or `~/.config/winbench/winbench.conf` |
| `WINBENCH_IMAGES=1` | screenshots are returned as images too (set for Claude Code by the installer) |
| `WINBENCH_LEASE=off` | no lease, for a VM only one session ever uses |
| `WINBENCH_SESSION_LABEL` | how this session is named to the others in the queue |

## Several sessions, one VM

Every MCP server process is a session, and only the session holding the lease may use the VM. The lease
is kept on the Proxmox host, so rolling the VM back cannot lose it.

- The first VM tool call takes the lease when the VM is free, so a single session never notices it.
- When another session holds it, VM tools fail with who holds it, for how long, how long it has been idle,
  and your place in the queue. You keep that place while your session runs; `vm_lock(wait_s=...)` waits
  for the VM, `vm_lock_status` checks, `vm_unlock` hands it on.
- **A forgotten lock frees itself:**
  - a session not heard from for 120 s is dropped (closed, crashed, laptop asleep); servers heartbeat
    every 30 s;
  - a holder that runs no VM tool for 15 minutes is dropped even while its session stays open.
    `vm_lock(idle_minutes=...)` sets 1 to 240 minutes, for long builds between VM steps;
  - a session releases its lease when it exits;
  - `vm_lock(steal=true)` takes over at once, when a person decides the holder is abandoned.
- The holder's results say when others are waiting. A session that gets the VM back after another one
  used it is warned that its programs, files and snapshots may be gone.
- `vm_status`, `vm_snapshots`, `vm_screenshot_host` and the lease tools work without the lease.
  `scripts/gate.sh lease status` shows the lease from a shell.
- The lease binds the MCP servers. It does not stop `scripts/rpc.sh` or a person at the VM console.

## Security model

- The model only calls MCP tools. The MCP server holds one ssh key, which reaches root on the host only
  through the gate (whitelisted operations on one VM id) and one TCP forward to the agent: no shell, no
  SFTP, no other forwards. ssh runs with `-F /dev/null`, `IdentitiesOnly` and no agent, so your own keys
  are never offered.
- The agent needs a bearer token; its firewall rule accepts only the VM's own network.
- Inside the VM the model is admin. The fence (`install-host.sh --fence`) filters by bridge port, so a model
  that changes the VM's IP or MAC still cannot reach other guests. Keep host services closed to the VM
  with the host firewall.
- Claude Code's and pi's own shell and file tools are not limited by winbench. For sessions that should
  touch only the VM, allow `mcp__winbench__*` and deny `Bash`.

## Troubleshooting

- **"agent not reachable":** the VM is rebooting, the desktop session is gone, or the agent died. The
  server starts the agent's task through the host and waits up to a minute; the call itself did not run,
  so repeat it. `vm_status` shows both agents, `vm_screenshot_host` shows the screen even without the
  agent, `vm_reset` hard-resets.
- **A UI call times out or is refused with "stopped in debugger":** a process sitting at a cdb prompt is
  frozen and UI Automation would wait on it forever. Let it run (`dbg_cmd(sN, "g")`) or `dbg_close`.
- **Logs:** MCP server `~/winbench/server.log`, tunnel `~/winbench/tunnel.log`, agent
  `C:\ProgramData\winbench\agent.log`, VM setup `C:\ProgramData\winbench\setup.log`, gate
  `/var/log/winbench-gate.log` on the host.
- **Long calls cut off:** the installer sets a 300 s tool timeout for Claude Code; for pi set
  `"timeout": 300` for winbench in `~/.pi/agent/mcp.json`.
- **Rollback fails:** ZFS rolls back only to the newest snapshot. `vm_delsnapshot` your own snapshots first.

## Tips for models and their users

- Element ids live until the next `screen_describe`; describe again after anything that changes the screen.
- `start()` of a packaged app (Notepad, Calculator) returns the launcher's pid; take the real pid from
  `list_windows` or `processes` before `dbg_open`.
- Windows OCR reads normal UI text well and fails on spell-check squiggles and very small fonts.
- A tool error that lists what exists ("no window matching ..., open windows: ...") is the model's chance
  to spot a misspelling. The server instructions tell it never to repeat an identical failing call.

## Development

```bash
uv venv .venv && uv pip install --python .venv/bin/python pytest psutil mcp
.venv/bin/pytest                 # agent, host and server tests; no Windows needed
scripts/push-agent.sh            # copy the agent into the VM and restart it
scripts/agent-smoke.sh           # drive Notepad end to end through the agent
scripts/debug-smoke.sh           # crash capture, dump analysis, live cdb
scripts/frozen-smoke.sh          # a debugger-stopped process must not hang the UI tools
server/.venv/bin/python scripts/smoke-mcp.py   # the MCP server over stdio, like a client
```

`scripts/push-agent.sh` restarts the agent, which ends every open `dbg_*` session.

## License

[MIT](LICENSE)