From ac1aab2e9114df2f65eb1713b515781655ac565b Mon Sep 17 00:00:00 2001 From: Brendan Davis Date: Mon, 23 Nov 2026 01:22:05 -0500 Subject: [PATCH 1/2] Add a design for the per-session dev server router Co-Authored-By: Claude Opus 5.5 --- docs/plans/dev-server-router.md | 485 ++++++++++++++++++++++++++++++++ 1 file changed, 485 insertions(+) create mode 100644 docs/plans/dev-server-router.md diff --git a/docs/plans/dev-server-router.md b/docs/plans/dev-server-router.md new file mode 100644 index 00000000..1888e28f --- /dev/null +++ b/docs/plans/dev-server-router.md @@ -0,0 +1,485 @@ +# Dev Server Router + +* **Author:** Brendan Davis +* **Status:** Draft +* **Date:** 2026-11-23 + +-------------------------------------------------------------------------------- + +## Overview + +A session gets a **Dev Server** button next to its VS Code, Files and Diff +buttons. The button opens `https://-.beta.workshop.chu.zone`, a +hostname that belongs to that session. Workshop receives every request for that +hostname, starts the session's dev server if it is not running, and proxies the +request to it. The command that starts the dev server is stored on the track. + +The dev server runs inside the Workshop container, on a port only Workshop can +reach. Nothing new is published from Docker. One wildcard DNS record and one +wildcard certificate cover every session. + +## The Problem + +Agents work in per-session git worktrees inside the Workshop container on +unraid. When the user wants to try what an agent built, someone has to start +the app's dev server and the user has to reach it from a browser. Today: + +* The container publishes only port 3002, so a dev server inside it is + unreachable from the LAN or from work. +* Publishing a port range (PR #57) makes servers reachable but forces the user + to track which session got which port. Those ports also have no + authentication in front of them. +* Starting a server is a manual chat request to an agent, with no fixed + command per project and no consistent place to find the URL. + +The user runs several projects (Workshop itself, Nozzle Works, others) and +often wants several sessions' servers open at once. + +## Goals + +* One click from a session opens that session's running app in a browser tab. +* A request to a session hostname whose server is not running starts it, then + shows the app once it answers. No separate start step. +* Any number of sessions can serve at the same time without the user choosing + or remembering ports. +* The same URL works at home and from work, behind Workshop's existing login. +* Each track stores its own start command: a single `npm run dev`, or a script + that starts several processes. +* A session's dev server stops when the session is deleted. + +## Non-Goals + +* Running dev servers in separate containers or through Docker. Workshop gets + no Docker socket. +* Production-like preview builds from CI (Gitea Actions). That is a separate + flow. +* Exposing more than one port per session. A backend on a second port must be + reached through the frontend's own dev proxy. +* Idle timeouts or resource limits on dev servers. +* Path-based routing (`/dev//...`). +* Native (non-Docker) installs at `localhost`. The router needs a wildcard + domain. Native installs keep starting servers by hand. + +## Open Questions + +* **Edge and certificate**: what terminates TLS for `workshop.chu.zone` + today? This decides how `*.beta.workshop.chu.zone` gets a certificate. + * *Cloudflare (proxy or tunnel)*: the free Universal SSL certificate + covers only `*.chu.zone`, one level deep. `*.beta.workshop.chu.zone` + needs Advanced Certificate Manager (paid), or the hostname scheme must + flatten to `-.chu.zone`. With that flatter scheme, other + `*.chu.zone` services share the wildcard route, so the tunnel ingress + rules must list the existing hostnames before the wildcard. + * *Nginx Proxy Manager, SWAG or Caddy on unraid*: a Let's Encrypt DNS-01 + challenge issues `*.beta.workshop.chu.zone`. The proxy forwards that + wildcard host to Workshop's port 3002 unchanged. This fails only if the + DNS provider has no API the proxy supports. + * *Tailscale only*: MagicDNS has no wildcard records. This needs a DNS + server (Pi-hole, AdGuard) set as Tailscale split DNS, and a certificate + from a DNS-01 challenge as above. Fails if no such DNS server exists. +* **Auth scope**: how does the browser prove it is logged in on a session + hostname? Exactly one of: + * *Shared cookie*: set `workshop_token` with + `Domain=.workshop.chu.zone`. One login covers every dev host. Every dev + app receives the Workshop token in its request cookies (the proxy can + strip it before forwarding, but app JavaScript on that origin cannot + read it because it is `httpOnly`). Fails if any dev app is untrusted, + since a server-side bug or a dependency in that app could log the + token. + * *Per-host handoff*: the button opens + `https:///__workshop/auth?t=`. Workshop + swaps the code for a host-only cookie and redirects to `/`. The main + token never leaves `workshop.chu.zone`. Costs a code store with expiry + and a redirect route. Fails for bookmarked or shared dev URLs opened in + a fresh browser: they show a "sign in through Workshop" page. +* **Workshop's own dev mode on a session**: Workshop's `npm run dev` binds + fixed ports 3001 and 5173, so two Workshop sessions cannot both serve, and + neither honours `$PORT`. A Workshop track command would need + `vite --port $PORT` with the backend on a derived port. To be settled when + the Workshop track's command is written; not a blocker for the router. + +## Critical User Journeys (CUJs) + +### CUJ 1: Open a session's app for the first time + +* **Given**: The track's dev command is `npm run dev -- --port $PORT`. The + session has a worktree. No dev server is running for it. The user is logged + in to Workshop. +* **When**: The user clicks **Dev Server** in the session's pane header. +* **Then**: A new tab opens at `https://nozzle-3f9a1c2e.beta.workshop.chu.zone`. + The tab shows a "Starting dev server…" page that reloads every two seconds. + Within the start timeout the app loads. Vite hot reload works in that tab. + The button shows a running indicator. + +### CUJ 2: Three sessions at once + +* **Given**: Three sessions on two tracks, each with a dev command. +* **When**: The user opens all three Dev Server links. +* **Then**: Each tab shows its own session's code. Workshop picked three + different free ports. The user never saw a port number. + +### CUJ 3: From work + +* **Given**: The user is away from home and reaches `workshop.chu.zone` + through the same edge as at home. +* **When**: They open a Dev Server link. +* **Then**: It behaves as at home. Without a valid Workshop login the + response is 401 and no server is started. + +### CUJ 4: The server fails to start + +* **Given**: The dev command exits with an error (missing dependencies). +* **When**: The user opens the Dev Server link. +* **Then**: The page shows the exit code and the last 50 lines of the + command's output, and a **Retry** link. It does not loop forever. + +### CUJ 5: Session deleted + +* **Given**: A session's dev server is running. +* **When**: The user deletes or soft-deletes the session. +* **Then**: The dev server process stops. The hostname returns 404. + +## Technical Plan (Architecture) + +```text +browser ── https://nozzle-3f9a1c2e.beta.workshop.chu.zone/... + │ + ▼ +edge (TLS for *.beta.workshop.chu.zone) ──► Workshop :3002, Host header unchanged + │ + ▼ +devServerRouter.middleware (first middleware; also hooked into 'upgrade') + ├─ Host does not end in .$WORKSHOP_DEV_DOMAIN → next() (normal Workshop) + ├─ no valid Workshop auth → 401 + ├─ (8 hex chars) matches no live session → 404 + ├─ track has no dev command → 404 "no dev command set" + ├─ devServers.ensure(session) + │ ├─ running and port answers → port + │ ├─ starting → 503 "Starting…" page, auto-reload + │ └─ exited → 502 page with exit code + log tail + └─ proxy HTTP / WebSocket to 127.0.0.1:, Host rewritten to 127.0.0.1: +``` + +### Hostname + +`-.`. `` is the first 8 hex +characters of the session UUID. The router parses only the last `-`-separated +part of the first label. The track slug is for humans and is ignored when +routing, so renaming a track does not break open tabs. The hostname has one +label under the base domain, so one wildcard certificate covers every session. +If the 8-character prefix matches more than one live session, the router +returns 409 and the button uses 12 characters for that session. With UUIDv4 +and tens of sessions, a collision is unlikely. + +`WORKSHOP_DEV_DOMAIN` (for example `beta.workshop.chu.zone`) is a new +environment variable. When it is unset, the router middleware does nothing and +the button is hidden. + +### Process supervision + +The dev server runs in a detached tmux session named `dev-`, with the +session's `workingDirectory` as its working directory and `PORT=` +in its environment. Its output is piped to +`~/.workshop/sessions//dev-server.log`. The chosen port and the +command are written to `~/.workshop/sessions//dev-server.json`. tmux +is chosen over a child process for three reasons: the terminal manager already +manages tmux sessions, the server survives a Workshop restart, and the user can +attach to it from a Shell session. After a restart, `ensure()` reads +`dev-server.json` and checks that the tmux session exists and the port answers, +then reuses the server. + +`ensure()` keeps one in-flight start per session in memory, so concurrent +requests (a page load fetches many assets) do not start the command twice. + +The command runs with no Host header rewriting by the app: the proxy sends +`Host: 127.0.0.1:`, which passes Vite's allowed-hosts check without any +project configuration. Hot reload works because Vite's client connects to the +page's own host and port when `server.hmr.clientPort` is unset. Projects that +set it explicitly must remove it or set it to 443. + +### Proxy + +The HTTP and WebSocket forwarding in `src/server/code-server-proxy.ts` moves +into a shared helper that takes a target port and a path. The code-server proxy +and the dev-server router both call it. Behaviour of `/code` stays the same. + +### Data model + +`tracks.dev_command TEXT NULL`. `Track.devCommand?: string`. Edited in track +settings as a single-line text field with the hint +"Runs in the session's workspace with `$PORT` set. Example: +`cd nozzle && npm run dev -- --port $PORT`". Multi-repo workspaces put the +`cd` in the command. + +## Alternatives Considered + +### Alternative A: Publish a port range per container (PR #57) + +* **Pros**: Tiny change. Works without DNS or certificates. +* **Cons**: The user tracks port numbers per session. No authentication on + the ports. Remote access needs Tailscale to reach raw ports. Fixed upper + limit on concurrent servers. +* **Reason for Rejection**: Rejected by author: the user does not want to + manage ports, and the ports are unauthenticated. + +### Alternative B: Traefik plus per-repo `compose.dev.yml` containers + +* **Pros**: Each dev server is isolated in its own container. Traefik routes + by Docker labels. Fits CI-built images from Gitea. +* **Cons**: Workshop needs the Docker socket, which is root on the unraid + host. Live-reload mode needs host paths for worktrees that live inside + Workshop's own volume. Every repo needs a compose file and labels. +* **Reason for Rejection**: Rejected by author: the user wants the server to + run from the session's local workspace through a Workshop button, not + through Docker. + +### Alternative C: Path-based routing under Workshop's host (`/dev//`) + +* **Pros**: No wildcard DNS or certificate. Reuses the existing cookie as is. +* **Cons**: Apps assume they are served at `/`. Vite needs `base` set per + project, absolute links and API calls break, and hot reload paths need + rewriting. +* **Reason for Rejection**: Rejected by author: it breaks most dev servers + unless each project is changed. + +### Alternative D: Two-level hostnames (`..beta.workshop.chu.zone`) + +* **Pros**: Reads naturally. +* **Cons**: A wildcard certificate covers one label only, so each track needs + its own certificate and DNS entry. +* **Reason for Rejection**: Rejected by author: one-label hostnames need one + certificate for everything. + +-------------------------------------------------------------------------------- + +## Non-Functional Requirements + +* **Security**: The router checks Workshop auth before it resolves a session + or starts anything. An unauthenticated request never runs a command. The + dev command is user-entered track data and runs as the Workshop user, the + same trust level as an agent's shell. The `Host` header is client-supplied. + A forged host only selects which session's server to reach, and auth is + still required. The proxy removes the `workshop_token` cookie and any + `?token=` parameter before forwarding, so dev apps do not log it. +* **Resources**: Each running dev server holds memory until its session is + deleted. There is no idle timeout (see Future Improvements). +* **Rollout**: Off unless `WORKSHOP_DEV_DOMAIN` is set. Rollback is unsetting + it. The `dev_command` column is additive. + +-------------------------------------------------------------------------------- + +### Additional Resources + +* `src/server/code-server-proxy.ts`: existing auth-gated HTTP and WebSocket + reverse proxy that this design generalises. +* `src/infrastructure/code-server/code-server.ts`: existing free-port and + wait-for-port helpers. +* [docs/docker.md](../docker.md): container layout and volumes. +* PR #57: the port-range approach this design replaces. + +### Future Improvements + +* **Idle stop**: stop a dev server after N minutes with no proxied requests. +* **Per-session override**: a session-level command that overrides the + track's. +* **Status in the session list**: show which sessions are serving. + +-------------------------------------------------------------------------------- + +## Detailed Implementation Plan + +### Target Files to Create/Modify + +#### 1. `src/server/reverse-proxy.ts` (Create) + +* **Type / Scope**: TypeScript module, server. +* **Proposed Changes**: The HTTP forwarding and WebSocket upgrade forwarding + from `code-server-proxy.ts`, as two functions: + `proxyHttp(req, res, port, path, headerOverrides)` and + `proxyUpgrade(req, socket, head, port, path)`. Both set + `Host: 127.0.0.1:` and `x-forwarded-proto` / `x-forwarded-host`. + `proxyHttp` removes `workshop_token` from the forwarded `Cookie` header. + No routing or auth. + +#### 2. `src/server/code-server-proxy.ts` (Modify) + +* **Type / Scope**: TypeScript module, server. +* **Proposed Changes**: Replace the inline forwarding with calls to + `reverse-proxy.ts`. Prefix, auth and availability checks stay. No + behaviour change. + +#### 3. `src/infrastructure/net/ports.ts` (Create) and `src/infrastructure/code-server/code-server.ts` (Modify) + +* **Type / Scope**: TypeScript modules, infrastructure. +* **Proposed Changes**: Move the existing private `findFreePort()` and + `waitForPort()` out of `code-server.ts` into `ports.ts` and export them. + `code-server.ts` imports them from there. The dev-server manager uses them + without depending on the code-server module. + +#### 4. `src/infrastructure/dev-server/dev-server-manager.ts` (Create) + +* **Type / Scope**: TypeScript module, infrastructure. +* **Proposed Changes**: + * `ensure(session, command): Promise<{ state: 'running', port } | { state: 'starting' } | { state: 'exited', code, logTail }>`. + Reads `dev-server.json` and checks the tmux session `dev-` and the + port. If none is running, picks a free port, writes `dev-server.json`, + and runs + `tmux new-session -d -s dev- -c -e PORT= " 2>&1 | tee ; echo __EXIT:$? >> "`. + Holds one in-flight start per session. Returns `starting` until the + port answers, and `exited` once the log has an `__EXIT:` line. + Start timeout: 120 seconds, after which it reports `exited` with the + log tail. + * `status(sessionId)`: same states, without starting anything. + * `stop(sessionId)`: `tmux kill-session -t =dev-`, deletes + `dev-server.json`. + * The command string goes to tmux as a single argument. No shell + interpolation by Workshop. + +#### 5. `tests/infrastructure/dev-server-manager.test.ts` (Create) + +* **Type / Scope**: Vitest. +* **Proposed Changes**: Uses a real tmux and + `python3 -m http.server $PORT --bind 127.0.0.1` as the command. Cases: + starts and returns `running`; a second `ensure` returns the same port; + concurrent `ensure` calls start once; a command that exits 1 returns + `exited` with its output; `stop` kills it. Skipped when tmux is absent. + +#### 6. `src/server/dev-server-router.ts` (Create) + +* **Type / Scope**: TypeScript module, server. +* **Proposed Changes**: `createDevServerRouter({ sessions, tracks, devServers, domain })` + returns `{ middleware, handleUpgrade }`, like the code-server proxy. + `parseDevHost(host, domain): string | null` returns the `` prefix. + The middleware runs the flow in the Technical Plan diagram. The + "Starting…" and failure pages are small inline HTML strings with + `` on the starting page. The + failure page HTML-escapes the log tail. If the per-host handoff option is + chosen, this file also serves `/__workshop/auth`. + +#### 7. `tests/server/dev-server-router.test.ts` (Create) + +* **Type / Scope**: Vitest. +* **Proposed Changes**: `parseDevHost`: valid host, host with port, wrong + domain, non-hex `sid`, uppercase. Middleware with a stub manager: non-dev + host calls `next`; no auth returns 401 and does not call `ensure`; unknown + `sid` returns 404; `starting` returns 503 with the refresh page; `running` + forwards. + +#### 8. `src/server/index.ts` (Modify) + +* **Type / Scope**: Server wiring. +* **Proposed Changes**: When `WORKSHOP_DEV_DOMAIN` is set, create the router + and mount its middleware first, before the code-server proxy and + `express.json()`, so request bodies reach the dev server unconsumed. Pass + it to `setupWebSocket`. + +#### 9. `src/server/websocket.ts` (Modify) + +* **Type / Scope**: Server wiring. +* **Proposed Changes**: In the `upgrade` handler, call + `devServerRouter.handleUpgrade` before `codeServerProxy.handleUpgrade`. + Without this, Vite hot reload WebSockets on dev hosts fall through to the + terminal socket handler. + +#### 10. `src/server/auth-routes.ts` (Modify, shared-cookie option only) + +* **Type / Scope**: Server. +* **Proposed Changes**: When `WORKSHOP_DEV_DOMAIN` is set, set and clear the + `workshop_token` cookie with `domain` equal to the parent of the dev domain + that also covers Workshop's own host (`.workshop.chu.zone`), derived from + `WORKSHOP_DEV_DOMAIN` minus its first label. `SameSite=Strict` still + works: subdomains of one registrable domain are the same site. + +#### 11. `src/foundation/types.ts` (Modify) + +* **Type / Scope**: Shared types. +* **Proposed Changes**: `Track.devCommand?: string`. A `DevServerStatus` + type for the status API: `'stopped' | 'starting' | 'running' | 'exited'`. + +#### 12. `src/infrastructure/persistence/database.ts` (Modify) + +* **Type / Scope**: SQLite migration. +* **Proposed Changes**: Guarded `ALTER TABLE tracks ADD COLUMN dev_command TEXT;`, + following the existing `worktree_mode` migration pattern. + +#### 13. `src/infrastructure/persistence/track-repository.ts` (Modify) + +* **Type / Scope**: Repository. +* **Proposed Changes**: Read and write `dev_command` in the insert, update + and row-mapping code. + +#### 14. `src/server/api.ts` (Modify) + +* **Type / Scope**: REST API. +* **Proposed Changes**: `PUT /tracks/:id` accepts `devCommand`. + `GET /sessions/:id/dev-server` returns `{ url, status }`, where `url` is + `null` when the feature is off or the track has no command. + `DELETE /sessions/:id/dev-server` stops it. `api.ts` is over the + 300-line file limit already, so these two routes go in a new + `src/server/dev-server-routes.ts` mounted from `api.ts`. + +#### 15. `src/server/dev-server-routes.ts` (Create) + +* **Type / Scope**: REST API. +* **Proposed Changes**: The two session routes above. Builds the URL from + the track name slug, the `sid` and `WORKSHOP_DEV_DOMAIN`. + +#### 16. `src/infrastructure/terminal/manager.ts` (Modify) + +* **Type / Scope**: Session lifecycle. +* **Proposed Changes**: `destroySession` and `softDeleteSession` call + `devServers.stop(sessionId)` before removing the worktree, so a server + never runs on a deleted directory. + +#### 17. `src/ui/workspace/WorkspaceButtons.tsx` (Modify) + +* **Type / Scope**: React UI. +* **Proposed Changes**: A **Dev Server** button, shown only when + `GET /sessions/:id/dev-server` returns a `url`. Clicking opens the URL in a + new tab (`target="_blank" rel="noopener"`). A small dot shows `running` + (green), `starting` (amber), `exited` (red), polled every 10 seconds while + the pane is visible. A secondary **Stop** action calls the DELETE route. + Accessible name: "Open dev server". + +#### 18. `src/ui/track-view/TrackConfig.tsx` (Modify) + +* **Type / Scope**: React UI. +* **Proposed Changes**: A "Dev server command" text input that saves + `devCommand`, with the `$PORT` hint from the Technical Plan. + +#### 19. `docker-compose.yml`, `docs/unraid/workshop.xml` (Modify) + +* **Type / Scope**: Deployment config. +* **Proposed Changes**: Optional `WORKSHOP_DEV_DOMAIN` variable, empty by + default. + +#### 20. `docs/docker.md` (Modify) + +* **Type / Scope**: Documentation. +* **Proposed Changes**: A "Dev servers" section: set + `WORKSHOP_DEV_DOMAIN`, point a wildcard DNS record and certificate at + Workshop for the chosen edge, write a track's dev command with `$PORT`, + and the `hmr.clientPort` note. + +#### 21. `docs/features.md` (Modify) + +* **Type / Scope**: Documentation. +* **Proposed Changes**: One entry describing the Dev Server button. + +### Verification Plan + +* **Compilation/Build**: `npm run typecheck`, `npm run build`. +* **Unit Tests**: `npm test`, including the new router and manager tests and + the existing `tests/server/code-server-proxy.test.ts` unchanged. +* **Static Analysis & Linting**: none configured (`npm run lint` is a stub); `npm run typecheck` covers it. +* **E2E/Integration**: Manual, on unraid with the wildcard DNS and + certificate in place: CUJ 1 with a Vite project (confirm hot reload by + editing a file), CUJ 2 with three sessions, CUJ 3 from a phone off Wi-Fi, + CUJ 4 with a deliberately broken command, and CUJ 5 by deleting a session + and checking `tmux ls`. + +-------------------------------------------------------------------------------- + + + +*Drafted with EGM* From 18c0ff24a7fa18690a5fd9a77e37f8b5017b54c2 Mon Sep 17 00:00:00 2001 From: Brendan Davis Date: Mon, 23 Nov 2026 01:48:41 -0500 Subject: [PATCH 2/2] Add per-session dev servers behind a wildcard hostname router A track's dev command now runs on demand for each session in tmux, and Workshop serves it at -.$WORKSHOP_DEV_DOMAIN through the same port 3002. A browser signs in to a dev host with a short-lived HMAC ticket, exchanged for a cookie that only that host receives. Workshop's own credentials are ignored on requests that come from dev-host pages. Co-Authored-By: Claude Opus 5.5 --- docker-compose.yml | 2 + docs/docker.md | 11 + docs/features.md | 1 + docs/plans/dev-server-router-review.md | 96 +++ docs/plans/dev-server-router.md | 626 +++++------------- docs/unraid/workshop.xml | 4 + src/domain/track/repository.ts | 2 +- src/foundation/dev-sid.ts | 7 + src/foundation/types.ts | 2 + src/infrastructure/code-server/code-server.ts | 41 +- src/infrastructure/dev-server/dev-server.ts | 163 +++++ src/infrastructure/net/ports.ts | 56 ++ src/infrastructure/persistence/database.ts | 7 + .../persistence/track-repository.ts | 16 +- src/infrastructure/terminal/auth-terminal.ts | 3 +- src/infrastructure/terminal/manager.ts | 82 +-- src/infrastructure/terminal/tmux-flags.ts | 74 +++ src/server/api.ts | 11 +- src/server/auth-middleware.ts | 22 + src/server/code-server-proxy.ts | 44 +- src/server/dev-host.ts | 56 ++ src/server/dev-server-router.ts | 183 +++++ src/server/dev-server-routes.ts | 30 + src/server/index.ts | 12 +- src/server/reverse-proxy.ts | 120 ++++ src/server/websocket.ts | 5 + src/ui/track-view/TrackConfig.tsx | 23 + src/ui/workspace/WorkspaceButtons.tsx | 52 +- tests/domain/track-repository.test.ts | 7 + tests/infrastructure/dev-server.test.ts | 74 +++ tests/server/dev-host.test.ts | 78 +++ tests/server/dev-server-router.test.ts | 144 ++++ tests/server/dev-server-routes.test.ts | 71 ++ 33 files changed, 1506 insertions(+), 619 deletions(-) create mode 100644 docs/plans/dev-server-router-review.md create mode 100644 src/foundation/dev-sid.ts create mode 100644 src/infrastructure/dev-server/dev-server.ts create mode 100644 src/infrastructure/net/ports.ts create mode 100644 src/infrastructure/terminal/tmux-flags.ts create mode 100644 src/server/dev-host.ts create mode 100644 src/server/dev-server-router.ts create mode 100644 src/server/dev-server-routes.ts create mode 100644 src/server/reverse-proxy.ts create mode 100644 tests/infrastructure/dev-server.test.ts create mode 100644 tests/server/dev-host.test.ts create mode 100644 tests/server/dev-server-router.test.ts create mode 100644 tests/server/dev-server-routes.test.ts diff --git a/docker-compose.yml b/docker-compose.yml index 13be6e01..3736c5ac 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,6 +30,8 @@ services: # Optional: Ollama server + model for the "Local (Ollama)" provider WORKSHOP_LOCAL_LLM_URL: "${WORKSHOP_LOCAL_LLM_URL:-}" WORKSHOP_LOCAL_MODEL: "${WORKSHOP_LOCAL_MODEL:-}" + # Optional: base domain for per-session dev servers (needs wildcard DNS + TLS, see docs/docker.md) + WORKSHOP_DEV_DOMAIN: "${WORKSHOP_DEV_DOMAIN:-}" volumes: # Persistent Workshop state (SQLite db, auth-token, sessions, vault, sprites) - workshop-state:/config diff --git a/docs/docker.md b/docs/docker.md index 6bf48eec..0866eee2 100644 --- a/docs/docker.md +++ b/docs/docker.md @@ -83,6 +83,17 @@ No `sshd`, no `docker exec`, no Docker socket mount. The "Open in Terminal" butt in the pane header is a *host*-side launcher (iTerm/Konsole/Windows Terminal) and is only useful for native, non-Docker installs. +## Dev servers + +A track can run a per-session dev server behind its own hostname. + +1. In the track's settings, set **Dev server command** (e.g. `npm run dev -- --port $PORT`). The server must listen on `$PORT`. +2. Set `WORKSHOP_DEV_DOMAIN` (e.g. `beta.workshop.chu.zone`). The feature is off without it. Only one Workshop instance should set it. +3. Point wildcard DNS `*.` and a wildcard TLS cert at Workshop's port 3002, preserving the `Host` header (Nginx Proxy Manager, SWAG, or Caddy with Let's Encrypt DNS-01). Cloudflare's free cert covers only one level, so with Cloudflare use a domain one level under the zone, or a paid cert. +4. Click **Dev Server** on a session. The first load shows "Starting...". A failure shows the log with Retry. The server stops when the session is deleted. + +The proxy sends `Host: 127.0.0.1:`, so Vite's host check passes, but projects that hard-code `server.hmr.clientPort` must drop it. Apps that redirect to absolute `127.0.0.1` URLs won't work through the proxy. + ## Per-user editor paths "Open in Editor" hands `` to your local editor. If you SSH into diff --git a/docs/features.md b/docs/features.md index 1df9da41..d231fbf1 100644 --- a/docs/features.md +++ b/docs/features.md @@ -254,6 +254,7 @@ A visual representation of session activity above the terminal panel. - **Nix devshell** — `flake.nix` provides Node.js 24 and tmux; `direnv allow` activates automatically - **Dev + prod simultaneously** — Dev server (`:3001`/`:5173`) and prod server (`:3002`) can run at the same time against separate databases +- **Per-session dev servers** — A track's dev command runs per session, proxied at `-.` via the **Dev Server** button (see `docs/docker.md`) - **Hot module replacement** — Vite HMR for fast UI iteration - **TypeScript strict mode** — Throughout; ESM imports; no `any` drift - **Vitest** — Test suite for application and domain logic diff --git a/docs/plans/dev-server-router-review.md b/docs/plans/dev-server-router-review.md new file mode 100644 index 00000000..d6fe2523 --- /dev/null +++ b/docs/plans/dev-server-router-review.md @@ -0,0 +1,96 @@ +# Consolidated Review: Dev Server Router + +* **Target:** `docs/plans/dev-server-router.md` (commit `ac1aab2`, branch `workshop/dev-server-router`) +* **Mode:** Consolidated, all stages (5-9), with no edits to the document between stages +* **Context given to reviewers:** `docs/docker.md`, `src/server/code-server-proxy.ts`, `src/infrastructure/code-server/code-server.ts` +* **Stage reports:** `zoom_out_report.md`, `comprehension_report.md`, `critic_report.md`, `intermediate_zoom_out_report.md`, `implementation_readiness_report.md` + +## Verdict + +**Not ready to build.** +- The architecture holds: one wildcard hostname, routed inside Workshop, a tmux-run track command, opt-in through an environment variable. No reviewer argued for replacing it. +- What fails is the detail: + - Two decisions are still open. + - The process-supervision spec has a real exit-code bug. + - The plan misses a domain file and the Nix environment flags. + - The plan never says how the new manager reaches the code that needs it. + +The Stage 8 reviewer judged which of the earlier stages' proposed fixes were worth their cost. This report follows those judgments. Where Stage 9 asked for machinery that Stage 8 rejected, the item is listed under **Contested**. + +## Verified against the code (by the orchestrator, not a reviewer) + +* **Confirmed:** `src/server/websocket.ts:68-75` authenticates every WebSocket upgrade by token alone. It never checks `Origin`. With a shared `Domain=.workshop.chu.zone` cookie, any page served from a dev host is same-site, so the browser would attach the cookie. That page could then open Workshop's terminal WebSocket and run shell commands. This turns the shared-cookie option from a privacy risk into **remote shell access for any dev app's front-end code**. See B2. +* **Refuted:** IR#5's claims that the upgrade path has no auth and that `extractToken` needs an Express `Request`. The central gate at `websocket.ts:69-75` runs first, and `extractToken(req: IncomingMessage)` already works on upgrades. +* **Refuted:** `tmux -e` needing a version check. The image ships tmux 3.3a, and `-e` needs 3.2. + +## 🔴 Blockers + +| # | Action | Sources | +|---|--------|---------| +| B1 | Decide the edge (what terminates TLS and issues `*.`) and record the final hostname scheme. Treat the DNS/certificate setup as a precondition checked before merge. | ZO#1, IC#8, Comp#1-2, D#1, IR#1, CD#11, ZO8-C5 | +| B2 | Decide auth scope. The evidence points to **per-host handoff**: a stateless HMAC ticket of (sid, expiry) keyed on the existing token, swapped for a host-only cookie. No code store and no login redirect. Once decided, delete the other branch (item 6 or item 10). | ZO#2, IC#1, D#2-3, St#3, ZO8-A2/B2, verified above | +| B3 | Fix process supervision. Stage 8 proposes the smallest fix: drop `tee`, the `__EXIT:` line and the log file. Use tmux `remain-on-exit` + `#{pane_dead_status}` for the exit code and `capture-pane` for the log tail. Write the command to a script file and run `bash ' }); + const res = await get('/', cookie()); + expect(res.status).toBe(502); + expect(res.body).toContain('<script>'); + expect(res.body).toContain('/__workshop/retry'); + }); + + it('proxies running servers, including /api, without Workshop credentials', async () => { + ensure.mockResolvedValue({ state: 'running', port: (upstream.address() as AddressInfo).port }); + const res = await get(`/api/x?token=${process.env.WORKSHOP_AUTH_TOKEN}&keep=1`, { + cookie: `${cookie().cookie}; workshop_token=zzz; app=1`, + 'x-forwarded-host': 'evil', + }); + expect(res.body).toBe('upstream'); + expect(seen?.url).toBe('/api/x?keep=1'); + expect(seen?.headers.cookie).toBe('app=1'); + expect(seen?.headers.host).toMatch(/^127\.0\.0\.1:/); + expect(seen?.headers['x-forwarded-host']).toBe(HOST); + }); + + it("passes an app's own ?token= through", async () => { + ensure.mockResolvedValue({ state: 'running', port: (upstream.address() as AddressInfo).port }); + await get('/x?token=abc&a=1', cookie()); + expect(seen?.url).toBe('/x?token=abc&a=1'); + }); + + it('answers a // path with a 4xx and keeps serving', async () => { + const res = await get('//'); + expect(res.status).toBeGreaterThanOrEqual(400); + expect(res.status).toBeLessThan(500); + expect((await get('/', {}, 'workshop.example')).body).toBe('workshop'); + }); +}); diff --git a/tests/server/dev-server-routes.test.ts b/tests/server/dev-server-routes.test.ts new file mode 100644 index 00000000..1c5190c2 --- /dev/null +++ b/tests/server/dev-server-routes.test.ts @@ -0,0 +1,71 @@ +import { describe, it, expect, beforeAll, afterAll, afterEach } from 'vitest'; +import express from 'express'; +import type { Server } from 'node:http'; +import type { Session, Track } from '@foundation/types'; +import type { SessionRepository } from '@domain/session/repository'; +import type { TrackRepository } from '@domain/track/repository'; +import { createDevServerRoutes } from '@server/dev-server-routes.js'; +import { devSid } from '@foundation/dev-sid.js'; +import { verifyDevTicket } from '@server/dev-host.js'; + +const SESSION_ID = '12345678-90ab-cdef-1234-567890abcdef'; +let track: Partial; +let session: Partial | null; + +const sessions = { findById: () => session } as unknown as SessionRepository; +const tracks = { findById: () => track } as unknown as TrackRepository; + +describe('dev-server routes', () => { + let server: Server; + let base: string; + + beforeAll(async () => { + process.env.WORKSHOP_AUTH_TOKEN = 'test-token'; + const app = express(); + app.use(createDevServerRoutes({ sessions, tracks })); + await new Promise((resolve) => { server = app.listen(0, resolve); }); + const addr = server.address(); + base = `http://127.0.0.1:${typeof addr === 'object' && addr ? addr.port : 0}`; + }); + + afterAll(() => { server.close(); }); + afterEach(() => { delete process.env.WORKSHOP_DEV_DOMAIN; }); + + async function get(headers: Record = {}): Promise<{ url: string | null }> { + const res = await fetch(`${base}/sessions/${SESSION_ID}/dev-server`, { headers }); + return (await res.json()) as { url: string | null }; + } + + function setup(devCommand?: string, deletedAt?: string): void { + track = { name: 'My Track', devCommand }; + session = { id: SESSION_ID as Session['id'], deletedAt }; + } + + it('is null when the domain env is unset', async () => { + setup('npm run dev'); + expect((await get()).url).toBeNull(); + }); + + it('is null when the track has no devCommand', async () => { + process.env.WORKSHOP_DEV_DOMAIN = 'dev.example.com'; + setup(); + expect((await get()).url).toBeNull(); + }); + + it('is null for a deleted session', async () => { + process.env.WORKSHOP_DEV_DOMAIN = 'dev.example.com'; + setup('npm run dev', '2026-01-01'); + expect((await get()).url).toBeNull(); + }); + + it('returns a signed auth url', async () => { + process.env.WORKSHOP_DEV_DOMAIN = 'dev.example.com'; + setup('npm run dev'); + const { url } = await get({ 'x-forwarded-proto': 'https, http' }); + const parsed = new URL(url ?? ''); + expect(parsed.protocol).toBe('https:'); + expect(parsed.host).toBe(`my-track-${devSid(SESSION_ID)}.dev.example.com`); + expect(parsed.pathname).toBe('/__workshop/auth'); + expect(verifyDevTicket(parsed.searchParams.get('t'), devSid(SESSION_ID))).toBe(true); + }); +});