Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ on:
- 'phase-*/*'
pull_request:

permissions:
contents: read

jobs:
verify:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -45,3 +48,59 @@ jobs:
- run: pnpm --filter @arielcharts/shared build
- run: pnpm exec playwright install --with-deps chromium
- run: pnpm test:e2e-workspace-ux

protected-collaboration:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- uses: pnpm/action-setup@v4
with:
version: 10.15.0
- uses: actions/setup-node@v4
with:
node-version: 24
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm exec playwright install --with-deps chromium
- run: pnpm test:e2e-collaboration

deploy-server:
# Deploy only the protected main branch, and only after every local and
# browser collaboration gate has passed. Pull requests never receive Fly
# credentials from this workflow.
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs:
- verify
- workspace-ux
- protected-collaboration
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
persist-credentials: false
- name: Verify deployment secrets
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
ROOM_COOKIE_SECRET: ${{ secrets.ROOM_COOKIE_SECRET }}
run: |
set -euo pipefail
test -n "$FLY_API_TOKEN" || { echo '::error::Missing required repository secret FLY_API_TOKEN.'; exit 1; }
test -n "$ROOM_COOKIE_SECRET" || { echo '::error::Missing required repository secret ROOM_COOKIE_SECRET.'; exit 1; }
- name: Set up Fly CLI
# Fly.io's official setup action, pinned to a reviewed source commit.
uses: superfly/flyctl-actions/setup-flyctl@ed8efb33836e8b2096c7fd3ba1c8afe303ebbff1
with:
version: '0.4.80'
- name: Stage the room-cookie signing secret
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
ROOM_COOKIE_SECRET: ${{ secrets.ROOM_COOKIE_SECRET }}
run: |
set -euo pipefail
flyctl secrets set --app arielcharts-server --stage ROOM_COOKIE_SECRET="$ROOM_COOKIE_SECRET"
- name: Deploy server
env:
FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
run: flyctl deploy --app arielcharts-server --config fly.toml --remote-only
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ The repo now includes the Phase 2 + Phase 3 implementation baseline:
- presence strip, activity feed, share URL, and connection status UI
- configurable server/websocket endpoints via `NEXT_PUBLIC_SERVER_URL` and `NEXT_PUBLIC_WS_URL`
- `apps/server` — Node.js TypeScript server with:
- modern-only MCP `2026-07-28` `POST /mcp` endpoint for named diagram tabs (`listSessions`, `getSession`, `createDiagram`, `readDiagram`, `writeDiagram`, `renameDiagram`, and `deleteDiagram`)
- protected-room creation, browser access exchange, key rotation, and cookie-gated Yjs/history routes
- modern-only MCP `2026-07-28` `POST /mcp` endpoint for the one room named by its bearer capability (`getSession`, `createDiagram`, `readDiagram`, `listDiagramHistory`, `readDiagramRevision`, `writeDiagram`, `renameDiagram`, `deleteDiagram`, and `restoreDiagramRevision`)
- `OPTIONS /mcp` preflight handling and CORS response headers for browser-origin MCP requests
- `/health` health endpoint
- `/ws/:roomId` Yjs-compatible websocket rooms
- cookie-authorized `/ws/:roomId` Yjs-compatible websocket rooms
- LevelDB-backed session persistence and cleanup timers
- origin allowlisting via `ALLOWED_ORIGINS`
- `packages/shared` — shared contracts and types consumed by both apps
Expand Down Expand Up @@ -61,9 +62,39 @@ Runtime variables used by `apps/server/src/lib/env.ts`:
- `CLEANUP_INTERVAL_MS` — cleanup timer interval
- `SESSION_TTL_MS` — idle session TTL before cleanup
- `ALLOWED_ORIGINS` — comma-separated browser/websocket origin allowlist
- `ROOM_COOKIE_SECRET` — long random production secret used to sign room cookies; required when `NODE_ENV=production`
- `ROOM_COOKIE_TTL_MS` — browser-access cookie lifetime, defaulting to eight hours
- `ROOM_COOKIE_SECURE` — set `true` in production
- `ROOM_COOKIE_SAME_SITE` — `Lax`, `Strict`, or `None`; use `Lax` for the canonical same-site deployment below
- `CLIENT_ADDRESS_PROFILE` — `none` locally/default; `fly` trusts only one valid `Fly-Client-IP` header and ignores `X-Forwarded-For`
- `UV_THREADPOOL_SIZE` — pinned to `2` on Fly to bound concurrent scrypt memory; do not increase it on the single machine

For local development, `ALLOWED_ORIGINS=http://localhost:3000` is sufficient for the default Next.js dev server.

### Production room-access topology

Use `https://arielcharts.donovanyohan.com` for the web app and
`https://api.arielcharts.donovanyohan.com` for HTTP, WebSocket, and MCP. They
are same-site subdomains, so the browser origin allowlist should contain the
web origin exactly:

```bash
ALLOWED_ORIGINS=https://arielcharts.donovanyohan.com
ROOM_COOKIE_SECRET=<long-random-secret>
ROOM_COOKIE_SECURE=true
ROOM_COOKIE_SAME_SITE=Lax
CLIENT_ADDRESS_PROFILE=fly
UV_THREADPOOL_SIZE=2
```

Each production scrypt verification uses roughly 128 MiB through Node's shared
libuv pool. A process-wide queued-work limiter remains follow-up security debt.

Set `NEXT_PUBLIC_SERVER_URL=https://api.arielcharts.donovanyohan.com` and
`NEXT_PUBLIC_WS_URL=wss://api.arielcharts.donovanyohan.com` in the web
deployment. Do not use wildcard origins, put a room key in a query string, or
use `SameSite=None` merely to bridge unrelated production domains.

## Local development

Install dependencies once:
Expand Down Expand Up @@ -116,13 +147,33 @@ pnpm --filter @arielcharts/web typecheck
pnpm --filter @arielcharts/web test
```

For the protected collaboration boundary, run:

```bash
pnpm test:e2e-collaboration
```

After a deployment to the canonical DNS names, run the production smoke gate
explicitly. It creates and rotates a real private room, so it refuses any
other target:

```bash
E2E_PRODUCTION_SMOKE=1 \
E2E_BASE_URL=https://arielcharts.donovanyohan.com \
E2E_MCP_URL=https://api.arielcharts.donovanyohan.com/mcp \
pnpm test:e2e-production-smoke
```

## Core HTTP and websocket contracts

- `POST /mcp` implements MCP protocol `2026-07-28`. It is modern-only: use `server/discover` instead of `initialize`; requests carry the MCP protocol envelope plus `MCP-Protocol-Version`, `Mcp-Method`, and (for `tools/call`) `Mcp-Name` headers. MCP transport sessions and `Mcp-Session-Id` are not used.
- ArielCharts collaboration is application state, not MCP transport state: pass an explicit `sessionId` and stable `diagramId` to diagram tools. Call `getSession` before creating a tab, and `readDiagram` before replacing or renaming one; mutations pass that latest value as `expectedRevision` and must re-read after a stale-revision error.
- `POST /api/rooms` creates the only kind of room: a protected one. It returns `{ session_id, room_key }`; share it as `https://…/s/<sessionId>#roomKey=<roomKey>`. The fragment is exchanged for a signed, HttpOnly room cookie and removed before the workspace mounts. A bare room URL shows the RoomGate instead of session content.
- `POST /api/rooms/:sessionId/access` exchanges a room key for that browser cookie. `POST /api/rooms/:sessionId/rotate` requires that cookie, returns a replacement key, advances the access version, and closes current room sockets. Keep the raw key in memory only; a cookie-only reload can open an already-authorized room but cannot re-share its key.
- MCP is room-scoped rather than a session directory. Configure `Authorization: Bearer <sessionId>.<roomKey>` on every MCP request. The bearer may access only that session; no `listSessions` discovery capability exists.
- ArielCharts collaboration is application state, not MCP transport state: pass the authorized `sessionId` and stable `diagramId` to diagram tools. Always call `getSession` before creating a tab, then pass its fresh session revision as `expectedRevision`. Immediately before writing, renaming, deleting, or restoring a tab, call `readDiagram` and use its latest diagram revision. On a stale-revision error, re-read, merge deliberately, and retry only with that fresh revision—never blindly overwrite.
- `OPTIONS /mcp` supports browser preflight for the protocol headers above.
- `/health` returns a simple readiness payload
- `/ws/:roomId` hosts Yjs collaboration rooms
- `/ws/:roomId` hosts Yjs collaboration rooms only after the browser has a valid room cookie

The detailed contract source of truth lives in `docs/shared-contracts.md` and `packages/shared/src/types.ts`.

Expand Down
13 changes: 13 additions & 0 deletions apps/server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,17 @@ PORT=4000
DATA_DIR=.data/arielcharts
CLEANUP_INTERVAL_MS=60000
SESSION_TTL_MS=1800000
# Local browser origin. Production: https://arielcharts.donovanyohan.com
ALLOWED_ORIGINS=http://localhost:3000
# Required in production; generate a unique, long random secret and do not commit it.
ROOM_COOKIE_SECRET=replace-with-a-long-random-secret
ROOM_COOKIE_TTL_MS=28800000
# Local defaults. Canonical Fly production uses ROOM_COOKIE_SECURE=true,
# ROOM_COOKIE_SAME_SITE=Lax, and CLIENT_ADDRESS_PROFILE=fly. No forwarded
# header is trusted locally.
ROOM_COOKIE_SECURE=false
ROOM_COOKIE_SAME_SITE=Lax
CLIENT_ADDRESS_PROFILE=none
# Fly production pins this to 2 because each scrypt worker uses roughly 128 MiB.
# Do not increase it on the single machine. Local Node may keep its default.
# UV_THREADPOOL_SIZE=2
1 change: 1 addition & 0 deletions apps/server/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ the internal tool boundary; `mcp-server.ts` exposes the modern MCP contract;

## Collaboration and protocol boundaries

- `room-access.ts` owns room capabilities: its verifier/access-version record is server-private LevelDB state, never Yjs, activity, history, logs, or a URL query. Authenticate HTTP, WebSocket, and MCP ingress before `SessionManager`; only explicit protected creation may create a session. Rotation increments the access version and must close live room sockets. Client-IP limits default to the socket address; only `CLIENT_ADDRESS_PROFILE=fly` may use a single validated `Fly-Client-IP`, never `X-Forwarded-For`.
- Awareness is live, per-connection presence. Do not put browser-local UI
state in awareness or durable document state. Each live socket owns its
claimed client ids; stale/idempotent foreign echoes are filtered, while
Expand Down
Loading
Loading