Skip to content

The agent that grows with you

docs: run worktree desktops side by side without port eviction

brooklyn!64a9b4326167
diff --git a/website/docs/developer-guide/worktree-ui-dev.md b/website/docs/developer-guide/worktree-ui-dev.md
index 4f3ab1f847b..464365f642a 100644
--- a/website/docs/developer-guide/worktree-ui-dev.md
+++ b/website/docs/developer-guide/worktree-ui-dev.md
@@ -55,52 +55,85 @@ htui() {
## `hgui` — desktop app from the worktree
-The desktop app is heavier: it needs `node_modules` at both the repo root and `apps/desktop/`, a Vite dev server pinned to port `5174`, and a Python backend. `hgui` wires all of it against the current worktree:
+The desktop app needs dependencies at both the repo root and `apps/desktop/`, a Vite server, and a Python backend. The stock `npm run dev` pins Vite to `5174`; Electron also defaults to CDP port `9222` and takes a single-instance lock on its user-data directory. Changing only the Vite port is not enough to run two desktops.
+
+This **zsh** example gives each launch an explicit slot (`HGUI_SLOT`, default `0`). Use a different slot in each terminal. It uses the [shared helpers](#shared-helpers) below and requires `lsof`:
```bash
-hgui() {
- local root deps desktop
- root="$(_hermes_root)" || { echo "hgui: not in a Hermes checkout" >&2; return 1; }
+hgui() (
+ local root deps desktop slot="${HGUI_SLOT:-0}" vite_port cdp_port port
+ [[ "$slot" == [0-9] ]] || { print -u2 'hgui: HGUI_SLOT must be 0-9'; return 1; }
+ vite_port=$((5174 + slot))
+ cdp_port=$((9222 + slot))
+ for port in "$vite_port" "$cdp_port"; do
+ if lsof -nP -t -iTCP:"$port" -sTCP:LISTEN >/dev/null 2>&1; then
+ print -u2 "hgui: port $port is busy; choose another HGUI_SLOT"
+ return 1
+ fi
+ done
+
+ root="$(_hermes_root)" || { print -u2 'hgui: not in a Hermes checkout'; return 1; }
deps="${HERMES_GUI_DEPS_CHECKOUT:-$HERMES_MAIN_CHECKOUT}"
desktop="$root/apps/desktop"
- # Borrow deps when locks match; otherwise install locally in the worktree.
if cmp -s "$root/package-lock.json" "$deps/package-lock.json"; then
- _hermes_link_deps "$desktop" "$deps/apps/desktop"
- _hermes_link_deps "$root" "$deps"
+ _hermes_link_deps "$desktop" "$deps/apps/desktop" || return 1
+ _hermes_link_deps "$root" "$deps" || return 1
else
( cd "$root" && npm ci ) || return 1
fi
- # Vite is fixed at 5174 — evict a stale session from another hgui.
- lsof -t -i:5174 >/dev/null 2>&1 && killport 5174
+ cd "$desktop" || return 1
+ export PATH="$desktop/node_modules/.bin:$root/node_modules/.bin:$PATH"
+ export HERMES_DESKTOP_HERMES_ROOT="$root"
+ export HERMES_DESKTOP_PYTHON="$HERMES_MAIN_CHECKOUT/.venv/bin/python"
+ export HERMES_DESKTOP_CWD="$root"
+ export HERMES_DESKTOP_DEV_SERVER="http://127.0.0.1:$vite_port"
+ export HERMES_DESKTOP_CDP_PORT="$cdp_port"
+ export HERMES_DESKTOP_USER_DATA_DIR="${XDG_CACHE_HOME:-$HOME/.cache}/hermes-hgui/slot-$slot"
+ # A userData override would otherwise also relocate the agent's home.
+ export HERMES_HOME="${HERMES_HOME:-$HOME/.hermes}"
+ export XCURSOR_SIZE=24
+
+ # Mirror the dev scripts, replacing their fixed ports. No repo edits needed.
+ concurrently -k -n "vite,electron" \
+ "node scripts/assert-root-install.mjs && npm run clean:renderer && vite --host 127.0.0.1 --port $vite_port --strictPort" \
+ "tsc --build tsconfig.electron.json && wait-on http://127.0.0.1:$vite_port && node scripts/bundle-electron-main.mjs --dev && electron ."
+)
+```
- # Electron often survives Ctrl+C without reaping its ephemeral backends.
- trap '_hermes_gui_cleanup "$root"' INT TERM EXIT
+For example, after setting `HERMES_MAIN_CHECKOUT` and sourcing the helpers:
- ( cd "$desktop"
- export PATH="$root/node_modules/.bin:$PATH"
- HERMES_DESKTOP_HERMES_ROOT="$root" \
- HERMES_DESKTOP_PYTHON="$HERMES_MAIN_CHECKOUT/.venv/bin/python" \
- HERMES_DESKTOP_IGNORE_EXISTING=1 \
- HERMES_DESKTOP_CWD="$root" \
- npm run dev )
-}
+```bash
+# Terminal 1: main checkout
+cd "$HERMES_MAIN_CHECKOUT"
+HGUI_SLOT=0 hgui
+
+# Terminal 2: an existing worktree
+cd /path/to/hermes-worktree
+HGUI_SLOT=1 hgui
```
-The desktop env vars it sets are all real backend-resolution knobs:
+Slot `0` uses ports `5174`/`9222`; slot `1` uses `5175`/`9223`. Slots are caller-assigned, not atomically reserved: always use distinct slots for simultaneous starts. Busy ports are rejected, never evicted. Use separate checkouts for separate builds because launches in the same checkout still share build outputs.
| Variable | Role in `hgui` |
|----------|----------------|
-| `HERMES_DESKTOP_HERMES_ROOT` | Runs the backend from **this worktree**, not the packaged/PATH `hermes`. |
-| `HERMES_DESKTOP_PYTHON` | Reuses the deps checkout's venv instead of re-resolving a Python. |
-| `HERMES_DESKTOP_IGNORE_EXISTING` | Ignores any `hermes` on `PATH` so it can't shadow the worktree. |
-| `HERMES_DESKTOP_CWD` | Opens the desktop chat rooted at the worktree. |
-
-Two footguns `hgui` handles that a bare `npm run dev` does not:
+| `HGUI_SLOT` | Helper-only slot number, `0`–`9`; not a Hermes setting. |
+| `HERMES_DESKTOP_HERMES_ROOT` | Runs the backend from this worktree, not the packaged/PATH runtime. |
+| `HERMES_DESKTOP_PYTHON` | Reuses the main checkout's Python environment. Adjust for an installation that uses `venv` rather than `.venv`. |
+| `HERMES_DESKTOP_CWD` | Roots new desktop work in the worktree. |
+| `HERMES_DESKTOP_DEV_SERVER` | Points Electron at this instance's Vite server. |
+| `HERMES_DESKTOP_CDP_PORT` | Gives each instance its own renderer debugging port. |
+| `HERMES_DESKTOP_USER_DATA_DIR` | Separates Electron's single-instance lock, browser storage, and desktop preferences. |
+| `HERMES_HOME` | Explicitly preserves the agent home despite the Electron user-data override. |
+
+Each slot starts with fresh desktop preferences and remembers them on later launches. This example does not copy browser storage, saved navigation, or backend ownership from a running app.
+
+:::warning Separate desktops are not separate agent data
+The default `HERMES_HOME` is shared: sessions, configuration, credentials, and profiles remain the same. Avoid editing the same conversation from both instances. For destructive tests or incompatible database migrations, pass a separate temporary `HERMES_HOME` and configure that sandbox independently.
+:::
-- **Port `5174` is fixed.** A second `hgui` collides with the first's Vite server; the helper kills the stale one first.
-- **Orphaned children.** Electron frequently survives `Ctrl+C` through `concurrently` without reaping the ephemeral `dashboard --port 0` backend or the Vite process. The `EXIT`/`INT`/`TERM` trap runs a cleanup that terminates the Electron shell, the `:5174` listener, and any `--port 0` dashboard it spawned.
+Quit the app normally or press Ctrl-C in its launching terminal. `concurrently -k` manages its own child commands, and Electron owns its backend shutdown. Do not add a global `killport`, `pkill electron`, or a sweep of all `serve`/`dashboard --port 0` processes: those can terminate another instance. Remove the old `_hermes_gui_cleanup` trap if replacing an earlier version of this helper.
## Shared helpers
@@ -120,18 +153,8 @@ _hermes_link_deps() {
[[ -d "$source/node_modules" ]] || return 1
[[ -e "$target/node_modules" ]] || ln -s "$source/node_modules" "$target/node_modules"
}
-
-# Reap ephemeral backends Electron leaves behind on exit.
-_hermes_gui_cleanup() {
- local root="$1"
- [[ -n "$root" ]] && pkill -TERM -f "${root}/apps/desktop/node_modules/electron" 2>/dev/null
- lsof -t -i:5174 >/dev/null 2>&1 && killport 5174
- pgrep -f 'hermes_cli\.main.*dashboard.*--port 0' 2>/dev/null | xargs -r kill -TERM 2>/dev/null
-}
```
-`killport` is a small helper of your own (`lsof -ti:$1 | xargs kill`); substitute your preferred incantation.
-
:::info Why link only when locks match
A symlink to a divergent `node_modules` is worse than no install — the worktree would build against packages its own lockfile never declared. Byte-comparing `package-lock.json` is the cheap, exact guard: same lock ⇒ safe to borrow; different lock ⇒ `npm ci` locally. Vite realpaths symlinks before enforcing `server.fs.allow`, which is why `apps/desktop/vite.config.ts` whitelists the real `node_modules` location.
:::