--- title: Bootstrapping a git worktree so the dev server and OAuth work date: 2026-06-17 category: developer-experience module: development workflow / dev environment problem_type: developer_experience component: development_workflow severity: high applies_when: - Creating or first working in a git worktree under .claude/worktrees/ - The dev server throws on startup in a worktree (e.g. COOKIE_SECRET error) - OAuth sign-in fails or behaves inconsistently in a worktree tags: [worktree, dev-server, oauth, environment-setup, bootstrap, pnpm] --- # Bootstrapping a git worktree so the dev server and OAuth work ## Context A fresh `git worktree` (e.g. the harness-created checkouts under `.claude/worktrees/`) is a clean checkout: only tracked files come along. The two things the dev server actually needs at runtime are **gitignored**, so they are absent in a new worktree: - `.env` — holds `COOKIE_SECRET`, `SECRETS_KEY`, `ADMIN_DIDS`, etc. - `data/` — holds the OAuth/atmo signing keys (`oauth-key.json`, `atmo-key.json`). - A database: the dev database is Postgres, and each worktree gets its own `airglow_` database on the shared compose server, cloned from `airglow_dev`. The failure is not a graceful degradation — it blocks all dev work in the worktree: - Without `.env`, config throws **on startup**: `COOKIE_SECRET must be at least 32 characters` ([lib/config.ts](../../../lib/config.ts)). The server never comes up. - Without `data/`, a fresh worktree regenerates new signing keys, which silently breaks OAuth sign-in consistency with main ([lib/auth/client.ts](../../../lib/auth/client.ts), [lib/atmo/keys.ts](../../../lib/atmo/keys.ts)). The trap: an agent (or person) starts the server in a worktree, sees the crash, and burns time hand-debugging a "missing secret" error that is really just unseeded environment. ## Guidance Before doing anything else in a new worktree, run: ```sh pnpm worktree:setup ``` This is a checked-in script ([scripts/worktree-setup.sh](../../../scripts/worktree-setup.sh), wired as the `worktree:setup` package script) that seeds the worktree from the main checkout: 1. Copies `.env` (only when the worktree has none). 2. Copies the OAuth/atmo signing keys into `data/`. 3. Starts the compose Postgres and creates `airglow_` from `airglow_dev` if it does not exist yet (`CREATE DATABASE ... TEMPLATE`, falling back to `pg_dump` and restore when a running main dev server holds a connection to the source). 4. Points `DATABASE_URL` in the worktree `.env` at that database. 5. Runs `pnpm install` (only when `node_modules` is missing). 6. Runs `pnpm db:migrate` (applies any migrations the branch adds on top of the cloned DB). Key design properties: - It locates the main checkout with `git rev-parse --git-common-dir` (the shared `.git` dir's parent), so there is **no hardcoded path** — it works from any worktree. - It is **idempotent** and a **no-op from the main checkout**, so it is safe to run reflexively whenever a worktree server won't start. - It **won't recreate an existing worktree database**, so a database you are testing migrations against is never clobbered. Keys are always refreshed; an existing `.env` is kept, but its `DATABASE_URL` is always rewritten. ## Why This Matters Worktrees are the default isolation mechanism for parallel/agent work here, so "the dev server won't start in a worktree" is a recurring, high-friction failure. The fix turns a multi-minute debugging detour (read the stack trace, realize secrets are missing, manually `cp` files, discover OAuth still breaks because keys differ, re-copy `data/`) into a single idempotent command. What must be provisioned is the gitignored `.env` + `data/` plus the worktree's own Postgres database; everything else is tracked and comes with the checkout. ## When to Apply - Immediately after creating or entering a new git worktree, before `pnpm dev`. - Any time a worktree dev server fails to start with a missing-secret/config error — run it instead of debugging by hand. - Any time OAuth sign-in misbehaves in a worktree (likely regenerated keys). ## Examples Before (manual, error-prone, and easy to get half-right): ```sh # server crashes: COOKIE_SECRET must be at least 32 characters cp ../../.env .env # path depends on worktree depth — fragile pnpm install pnpm db:migrate # OAuth still broken — forgot data/oauth-key.json and data/atmo-key.json ``` After: ```sh pnpm worktree:setup # seeds .env + data/, clones the DB, installs, migrates; idempotent pnpm dev ``` ## Related - AGENTS.md → "Working in a git worktree" documents the one-command flow. - The dynamic-port fix (make the dev port derive from `PORT`, default 5176) is the sibling DX fix that keeps OAuth working when the worktree's server runs on its own port. A worktree inherits port 5176 from the tracked `launch.json`; if another server already holds 5176, kill the stale one.