Skip to content

Instantly share code, notes, and snippets.

@richardwei6
Created September 17, 2026 02:21
Show Gist options
  • Select an option

  • Save richardwei6/8497cfaee72edd75a4f3f46708077a59 to your computer and use it in GitHub Desktop.

Select an option

Save richardwei6/8497cfaee72edd75a4f3f46708077a59 to your computer and use it in GitHub Desktop.
devbox-setup

Running Claude Code / Codex on a cloud VM (but it feels local)

A barebones Linux box hosts every agent session. Local shell functions replace the claude and codex commands, ssh in, and pipe the TUI back into a Warp pane — icon, status badge and all. Close the laptop, the agents keep running.

┌──────────────────────────── your Mac ─────────────────────────────┐
│  Warp pane                                                        │
│    $ claude              ← zsh function, not the real binary      │
│          │                                                        │
│          ▼                                                        │
│    agent-proxy.py        ← rewrites pasted/dropped file paths     │
│          │                                                        │
└──────────┼────────────────────────────────────────────────────────┘
           │  ssh -t   (ControlMaster auto · keepalive 15s)
┌──────────▼──────────────────── VM ────────────────────────────────┐
│  tmux new-session  claude-<dir>-<n>                               │
│       └── claude   cwd → ~/trees/<repo>/<slug>                    │
│                                                                   │
│  ~/<repo>    read-only mirror                                     │
│  ~/trees/*   disposable worktrees   ← reap-trees   (hourly)       │
│  tmux        detached sessions      ← reap-agents  (every 15m)    │
└───────────────────────────────────────────────────────────────────┘

Box: 8 vCPU / 16 GB / 100 GB, Ubuntu 24.04, one setup.sh provisions it. Only the browser steps are manual (agent login, MCP oauth).


1. SSH config

Host dev
  HostName my-devbox
  RequestTTY yes
  ControlMaster auto
  ServerAliveInterval 15
  ServerAliveCountMax 3

Host dev-tunnel           # same, plus whatever you need locally
  HostName my-devbox
  LocalForward 4000  localhost:4000
  LocalForward 55432 localhost:55432

2. The wrappers

claude and codex are zsh functions, not aliases. Each opens a new tmux session on the box and attaches to it, so you never land back in a stale view.

claude() { _agent_remote claude "$@" }   # → tmux new-session claude-<dir>-<n>
codex()  { _agent_remote codex  "$@" }
alias claudel='command claude'           # local escape hatch
alias codexl='command codex --yolo'

They must be spelled exactly claude / codex. Warp matches the first token you typed against a hardcoded list before it attaches the listener that consumes the agent's OSC 777 warp://cli-agent events. Name the wrapper cc and the session still works — but you lose the brand icon and the working/done badge. Same reason ssh -t host 'claude …' never showed one: the first token is ssh.

3. Paste & drag-drop across the ssh boundary

A small Python proxy sits between your keyboard and ssh:

keystrokes ──────────────────────────────────► ssh    (untouched)
ESC[200~ …pasted block… ESC[201~ ──► inspect ─► ssh
                                       │
                                  is it a local file?
                                       │ yes
                                  upload, forward the REMOTE path

It only ever parses bracketed-paste blocks, so the agent's own escape sequences pass through — that's what makes it safe in front of a full-screen TUI. Any failure falls back to a plain ssh exec, so the worst case is "paths aren't rewritten", never "session is broken".

4. Session lifecycle

claude ──► new tmux session ──► Ctrl-b d ──► detached, still running
               │                                │
            /exit                          silent 12h
               │                                │
               ▼                                ▼
         session ends                  reaped  →  claude -r resumes it
what happened result
exit the agent session ends on its own
detach (Ctrl-b d) survives, agent keeps working
ssh drops / laptop closes survives — same as detach
detached and silent 12h reaped; conversation kept in ~/.claude
command
agents list sessions — handle, status, title
agents monitor that listing, live
agents <handle> attach
agents delete <handle|all> tear down

Trap: key "silent" on #{window_activity}, not #{session_activity}. The session field only moves while a client is attached, so "silent" collapses into "detached" and the reaper kills a 12-hour job precisely because nobody was watching it. tmux also has no "when did this detach" field (#{session_last_attached} is the attach moment and never moves), so the sweep records first-seen-detached in its own state file.

5. Worktrees

Several agents share one filesystem. The expensive failure is two of them in the same checkout — overwriting edits, stashing each other's work, switching each other's branches. The loser finds out afterwards.

~/research-os                        read-only: grep it, git log it, never commit
~/trees/
  research-os/
    fix-search-timeout/              ← agent A
    bump-pg-search/                  ← agent B
worktree research-os fix-search-timeout   # make or re-enter — idempotent
worktree --list
worktree --drop research-os fix-search-timeout

Everything under ~/trees is disposable by location. An hourly sweep removes a tree only when all four hold:

guard tree is kept when
in use a tmux pane's cwd is inside it
dirty git status --porcelain says anything
unmerged its commits aren't provably in the default branch
active anything under it was written in the last 24h

"Provably" matters under squash-merge: the branch's commits get rewritten, so merge-base --is-ancestor calls it unmerged forever. Ask GitHub instead — gh pr list --head <branch> --state merged — and accept it only when the merged PR's headRefOid is the exact commit in the tree.

6. Auth — the one to copy

Sign the box in on the box with claude auth login. Headless falls through to the paste-a-code flow on its own; the callback binds an ephemeral port, so no forward is needed.

Do not use claude setup-token + CLAUDE_CODE_OAUTH_TOKEN. It mints inferenceOnly, and the env var beats the stored credential. The bootstrap endpoint starts returning 403, and /model then silently serves its last cached model list — forever. Mine sat on a three-week-old catalogue that way, which reads exactly like an out-of-date CLI and isn't.

Two more, briefly:

  • Keep the box and the laptop on different accounts — a usage limit on one doesn't stop the other.
  • Give the box a scoped ServiceAccount for cluster access, never an account-wide cloud API token. An agent session holding one can delete the VM it's running on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment