Skip to main content
Glama
README.md
# claude-in-gcp

Hosts a local Claude Code session on a Google Cloud VM so it carries on as if local: same id, transcript, paths, skills and local MCP servers. One local daemon on `http://localhost:18020` serves the REST API and MCP endpoint (`/mcp`), and connects to the VM whenever it is up.

| Path | What |
|---|---|
| `src/local/` | Daemon: API, MCP tools, VM control, SSH link, Mutagen sync, MCP gateway, peer proxy |
| `src/cloud/agentd.ts` | Runs on the VM: one Agent SDK runner per session |
| `cloud/` | Image, entrypoint, provisioning, model router |
| `bin/` | `agent-cloud-sync`, `install-opencode-key` |

Build: `npm install && npm run build`. Test: `npx vitest run` (some suites skip when the optional companion tools are absent). Seven suites (44 tests: `broker-address`, `gitgate`, `handoff`, `observe.hard`, `observe.service`, `watch-ready`, `debuglog`) predate the latest session-gating changes and still expect the old behaviour; they are excluded from the default run and can be run with `CGCP_ALL_TESTS=1 npx vitest run`. Service: `systemd/claude-in-gcp.service`. After changing the code, restart the service and `POST /api/vm/redeploy`.

## Architecture

```mermaid
flowchart LR
  CC[Claude Code on this computer] -->|MCP, /mcp| D[local daemon :18020]
  D -->|SSH link| V[Compute Engine VM]
  D <-->|Mutagen live sync| V
  subgraph VM
    A[agentd] --> R1[Agent SDK runner<br/>one per session]
    A --> R2[Agent SDK runner]
  end
  V --- A
  D --> GW[MCP gateway<br/>the session's local MCP servers]
  D --> SH[read-only shares<br/>rclone over a reverse SSH forward]
```

A session is moved to the VM and keeps its id, transcript and paths; the local MCP servers it used stay reachable through the gateway. `watch_session` returns only when the session is uploaded, hosted, the link is up and every MCP server of the session is connected.

## MCP tools

The daemon's MCP endpoint offers 17 tools:

| Group | Tools | What they do |
|---|---|---|
| Sessions | `watch_session`, `unwatch_session`, `list_watched`, `search_sessions`, `sync_session_to_local` | Move a local session to the cloud and watch it, stop hosting it, list what is hosted, find the sessions saved for a folder, copy a cloud session back |
| Talking to a session | `send_message`, `send_perch_message`, `interrupt_session`, `switch_session_model` | Delegate to a hosted session or send to a local one, prompt a Perch session, interrupt, change the model from the next message |
| Reading a session | `get_session`, `view_cli`, `debug_log` | Bounded latest transcript events, the full input and output of one tool call, one filtered read of the daemon's own log |
| The VM | `start_vm`, `terminate_vm`, `vm_status` | Start or resize the VM (it bills while running), stop it (disk and sessions are kept), show its state, link and sync health |
| Housekeeping | `compact_after_usage_limit`, `subscribe` | Compact watched sessions after a usage limit, subscribe a Workflow project to a system of references (the same subscription the Workflow tool writes) |

## Requirements and caveats

- Node.js 22 or newer, `npm`.
- A Google Cloud project with a Compute Engine VM you control, the `gcloud` CLI signed in, and SSH access to the VM. VM names, zones and keys are read from local configuration (`src/local/config.ts`) and are never committed.
- On this computer: Mutagen (live folder sync), and for read-only shares `rclone`; on the VM: Docker and `sshfs`.
- The optional `CGCP_PROXY_FILE` points at a second model proxy script that is copied into the VM image; without it the image build step that copies it fails, so set it or remove that line.
- The web UI that used to ship beside the daemon is not part of this release; the daemon serves the REST API and MCP endpoint only.
- Several suites exercise companion tools (a reference-reading system, a workflow server, a research-session server). They skip or are mocked when those tools are absent; two fixtures (`src/local/fixtures/`) are vendored so the communication-rule tests run alone.
- Perch account failover reads a local accounts file (`group_1.accounts.json`, mode 600, gitignored). Copy the shape from `src/local/perch-accounts.ts`; no accounts are included.

## Perch sessions

A model string `perch-deepseek-flash` (in `watch_session`, `POST /api/sessions`, or the new-session form on the Perch tab) starts a Perch session in a folder instead of moving a Claude one: it runs `perch acp` (Perch's Agent Client Protocol server) on this computer, never on the VM, and never through the Agent SDK.

| What | Where |
|---|---|
| Streamed turns, session continuation, interrupt | `src/local/perch.ts` (one `perch acp` process per turn; the ACP session id is the row's `thread`) |
| Background shells (live output, stop), MCP list, model check | `src/local/perch-state.ts`, reading `~/.perch` (`background-tasks.json`, `background-output/`, `mcp.json`, `threads/`) |
| Prompting a Perch session from another session | `send_message` to its title or id |
| Install the binary | `bin/install-perch [version]` (versioned folder + `~/.local/bin/perch`) |

Perch's login stays on this computer (`~/.perch` is never synced and never a working folder). Perch loads MCP servers from `~/.perch/mcp.json` and `<folder>/.perch/mcp.json` on this computer; ACP ignores the `mcpServers` a client passes, so the Claude gateway tools (`list_agents`, `send_message`) are not available inside a Perch session. Perch has no monitor tool: a shell a turn is waiting on (`wait_for_background_task`) is shown as a monitor.

### Perch account failover

`group_1.accounts.json` (next to `package.json`, mode 600, never synced) lists Perch accounts in order: a `label`, the Google `email` and the account's login folder `authDir` (its `PERCH_CLI_AUTH_DIR`; account 1 is `~/.perch`, the others `~/.perch-accounts/group_1/<label>`). Each turn runs on the first account that is signed in and not limited (`src/local/perch-accounts.ts`). When Perch says the account hit a limit (its own sentences: 5-hour, weekly, monthly, "needs a plan", rate limit), or sends nothing for 240 s with no tool running, the turn stops at once and the transcript says so: `Usage limit hit on Perch account g1-1 (...)`, then `Failing over: this turn runs again on Perch account g1-2`. The turn runs again there, in a new Perch session that gets a note saying why plus the last messages. When no account is usable the turn fails with `usage limit: no usable Perch account in group_1 (limited: ...; not signed in: ...)`, never a bare time-out. Limits and the account behind each thread are kept in `run/perch-accounts.json` (labels only). Sign in an account with `bin/perch-login-account <label>` (prints the Google sign-in URL; Perch waits about 2 minutes for it) and check it with `bin/perch-login-account <label> --check`.

## Read-only local shares (any project)

Cloud agents can read local folders (an OneDrive archive, a dataset, a reference tree) without copying them to the VM.
- **Register:** add `"<name>": "<absolute local folder>"` to `shares.json` → `{"shares": {...}}`. The file is local only, mode 600, and gitignored; `CGCP_SHARES_FILE` overrides its location. Changes take effect within 30 s, with no restart.
- **Where agents see it:** `/home/user/cloud-shares/local/<name>` inside the agent container, read-only.
- **How it works:**
  - `bin/cloud-shares` (user unit `systemd/cloud-shares.service`) runs `rclone serve sftp --read-only` over `run/shares/<name>` symlinks on 127.0.0.1:2233, with a reverse SSH forward to the VM.
  - On the VM, `cloud/cgcp-shares.service` mounts it with sshfs at `/mnt/cgcp-shares/local`.
  - `cloud/provision.sh` binds `/mnt/cgcp-shares` into the container with `rslave` propagation.
- **Never shared:** key stores, `.claude`, `.config`, Perch logins, `payments-app/run`, `/mnt/c/Data/keys`. Invalid entries are skipped and logged.
- **First install on a VM:** copy `cloud/cgcp-shares.service` to `/etc/systemd/system/`, then `systemctl enable --now cgcp-shares`. Recreate the container (`provision.sh`) so the bind exists.