feat: add the multiplayer room server and client

A room is one conversation, one ssh sandbox, and one agent. Accounts
create rooms; friends join through a single-use invite link and a
display name; the agent speaks only through the message tool.

The server is Axum and SQLite. It serves the built Nuxt client, streams
room events over SSE, and runs one serialized turn loop per room with
turn-level steering copied from rhema-code. The client has the chat,
preview iframe, read-only file pane, xterm terminal, and presence.

Preview URLs are signed wildcard hosts, and the proxy tunnels HTTP over
ssh direct-tcpip. HostAgent::step remains deferred in the framework.
This commit is contained in:
vhaudiquet committed 2026-11-28 01:09:37 +01:00
1 parent 04dd63c4ca
commit 0035f4e031
69 files changed
+22501 -48

No files matched your search

+85 -48
View File
@@ -1,6 +1,7 @@
# rhema-together
> **Status:** draft, discussed with the author. No code yet.
> **Status:** v1 implemented in this repository. The sections below are
> the design; §12 records where v1 differs.
> **Framework:** consumes the Rhema crates at the workspace-pinned
> version. Adds nothing to `rhema-core`.
@@ -49,20 +50,19 @@ A room holds:
| field | meaning |
|---|---|
| id | ULID, used in URLs |
| id | 12-character base32, used in URLs |
| name | display name, editable by the owner |
| owner | the account that created it |
| workspace | the sandbox directory the room lives in |
| cwd | the checkout the agent starts in, relative to workspace |
| sandbox_target | the owner's ssh target |
| provider_config | whose model key pays |
| model | model id, may change per room |
| max_turn_steps | runaway guard, default 2048 |
| max_turn_steps | runaway guard, default 48 |
| created_at, archived_at | |
The workspace can hold several git checkouts. The agent's `cwd` is one of
them, and the file tree and preview show the workspace root. Seeding is a
`git clone` into the workspace on first use, or an empty directory the
The workspace can hold several git checkouts. The agent starts at the
workspace root, and the file tree and preview start there too. Seeding is
a `git clone` into the workspace on first use, or a README seed the
first member fills.
The owner picks the provider and the model, and only the owner changes
@@ -304,8 +304,7 @@ call in v1.
preview ports, uploads)
```
The agent is started with `cwd` set to one checkout. The file tree and
preview start at the workspace root, so a room that grows a second
The agent starts at the workspace root, so a room that grows a second
checkout shows it without a new room.
## 6. The panes
@@ -333,17 +332,23 @@ Iframe at a preview URL. Discovery is a manual port field plus a scrape
of `bash` output for `localhost:<port>` and `127.0.0.1:<port>` shown as
suggestions. No heuristics beyond that.
Serving: a harness listener per (room, port) tunnels through ssh
`direct-tcpip` to the sandbox. TLS and routing happen in the reverse
proxy.
Serving: the app's listener recognizes preview hosts and tunnels each
request over ssh `direct-tcpip` to the sandbox. The ssh connection is
opened once per sandbox target and reused by every room and port on it.
TLS and DNS happen in the reverse proxy.
Layout, one domain and one certificate:
Layout, one wildcard record:
```
app https://together.<domain>/room/<id>/
preview https://preview.together.<domain>/room/<id>/port/<port>/
preview https://r<room>-p<port>-<sig>.preview.<domain>/
```
The room, port, and a 24-hex HMAC signature form the first label. The
signature is minted by the app when the pane opens and checked by the
proxy, so the URL is not guessable from a room id. It does not expire;
treat a copied preview URL as a room read capability.
The preview needs its own hostname. It is repo code, and on the app's
origin it could call the API with the viewer's cookie, which turns every
opened preview into an account-stealer for the other members. A separate
@@ -353,41 +358,34 @@ and preview responses carry
The iframe carries `sandbox="allow-scripts allow-same-origin allow-forms
allow-popups"`.
The path prefix is the fragile part. Apps that emit root-absolute URLs
(`/assets/app.js`) escape it, and JS-constructed URLs escape any rewrite.
The proxy strips the prefix, rewrites HTML `src`/`href`/`action`, CSS
`url()`, and `Location`, and passes `X-Forwarded-Prefix`. Vite and Next
still need their `base` configured. The preview pane shows a hint with
the port's URL when the page looks broken.
Each room+port gets its own hostname, so root-absolute asset URLs
(`/assets/app.js`), JS-constructed URLs, and `Location` headers need no
rewriting. Vite's HMR WebSocket is not proxied in v1; reload with the
button after an edit, or disable HMR in the previewed server.
If the prefix proves too fragile, move to
`https://<room>-<port>.preview.<domain>/` with a wildcard DNS record and
on-demand certificates. That swaps a proxy route, not app code.
Auth: the preview host carries no app cookie, and the signed label is
the whole capability. Responses are not framed by policy on the preview
side; the app's CSP names `*.<preview host>` in `frame-src`.
Auth: the preview host carries no session cookie, so the proxy needs
another answer. The app mints a short-lived signed token when the pane
opens (`?k=<token>`, or a path segment); the proxy checks it, strips it,
and forwards. One hour, bound to the room and the member. An unguessable
per-room token with no expiry is the simpler fallback, at the cost of a
link that works for anyone who holds it, forever.
Refresh: a button, plus a reload when a turn ends, plus the dev server's
own HMR.
Refresh: a button and a reload when a turn ends.
### Terminal
One PTY per tab, created by a member inside the room's sandbox. Tabs are
private by default; a share toggle lets others watch, and typing stays
with the owner until someone flips it.
One PTY per browser connection, created inside the room's sandbox over
the same ssh target and key the agent uses. v1 has one visible terminal
per room; opening a second tab creates a second live shell with its own
scrollback. Nothing is shared into the room log.
Transport: raw bytes over a WebSocket, binary frames for output, JSON
control frames for resize, close, and title. The server keeps a 1 MB ring
per tab and replays it on reconnect. Killing the tab closes the channel,
which SIGHUPs the remote shell.
Transport: raw bytes over a WebSocket. Output arrives as binary frames;
input may be binary, or JSON `{"data": "..."}`; resize is JSON
`{"type":"resize","cols":N,"rows":N}`. The server sends
`{"type":"ready"}` once the PTY is up, and `{"type":"exit","code":N}`
when the shell ends. There is no server-side ring buffer; a reconnect
starts a fresh shell. Closing the socket closes the channel, which
hangs up the remote shell.
The emulator runs in the browser, from bytes. Ghostty's WASM build if its
API fits, xterm.js otherwise. The server parses no escape sequences in
v1. A server-side libghostty-vt is a later option: it gives screen
The emulator is xterm.js in the browser. The server parses no escape
sequences. A server-side libghostty-vt is a later option: it gives screen
snapshots on reconnect and a way to let the model read the terminal, and
it costs an FFI crate that allows `unsafe`. That crate would live in this
project, not in the framework.
@@ -624,15 +622,54 @@ Settled:
shell-on-top runner as a possible degraded mode.
2. Provider and model: the room owner picks both, and only the owner
changes them. Every turn spends from that config.
3. Interrupts: any member, rate-limited.
3. Interrupts: any member. v1 has no rate limit.
4. Preview auth: a per-room, per-port signed host label with no expiry.
A copied preview URL is a read capability for that port.
5. Terminal sharing: private shells, one per browser connection. The
room log does not record terminal contents.
Still open:
4. Preview auth: a short-lived signed token minted per pane open
(recommended), or an unguessable per-room token with no expiry?
5. Guest lifetime: cookie forever, or an account required after N days?
6. Terminal sharing: private tabs with an opt-in watcher, or one shared
shell per room?
6. Guest lifetime: cookie forever, or an account required after N days?
7. Does the agent get tools to read the terminal and the preview later?
8. Email: skip in v1 with operator password resets, or add SMTP from day
one?
## 14. What v1 ships
This repository, as committed:
- Axum server, SQLite, and the built Nuxt client served from the same
listener.
- Accounts (first account through `/setup`), password login, guest
accounts through single-use invite links with a display name.
- Rooms with members, a per-room provider config, a sandbox target, a
workspace, an optional repository URL, and an owner-only model and
provider switch.
- One conversation per room, a `@rhema` mention starts a turn, human
messages are stamped with their author's name, and only the `message`
tool puts agent text in the room.
- Turn-level steering: a new mention drops the running turn (status
`interrupted`) and starts a fresh one over the committed session.
`HostAgent::step` is not in the framework; the turn-level pattern from
`rhema-code` is copied here.
- SSH sandboxes through `rhema-sandbox-ssh`, with the account's key
encrypted at rest, host key learned on first connect. The agent, the
files pane, and the terminal share one target.
- The `message` tool, a prompt with room rules, per-turn usage recorded
in `turn_end`.
- SSE event feed with durable replay (`?since=`) and transient deltas;
a WS presence socket with online and typing state.
- Read-only files tree and file view, `git status` and `git diff`.
- xterm.js terminal over a WebSocket PTY.
- Preview proxy over ssh `direct-tcpip`, signed wildcard hosts, no
rewriting.
- No approval prompts: the sandbox is the boundary, so `AllowAll`.
Not in v1: ecrou, multiple checkouts in the UI, port discovery, HMR
through the preview proxy, a terminal ring buffer, editor cursors,
co-editing, email, account recovery, rate limits, and desktop windows.
The schema is created by `src/db.rs`; the routes are in
`src/api/mod.rs`; the environment variables are in `src/config.rs` and
`README.md`.