Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

docs(rpc): add README for the RPC server module

Steven Enamakel7561cad71faf
diff --git a/crates/openhuman-rpc/src/server/README.md b/crates/openhuman-rpc/src/server/README.md
new file mode 100644
index 0000000000..a8e1c646dd
--- /dev/null
+++ b/crates/openhuman-rpc/src/server/README.md
@@ -0,0 +1,213 @@
+# server
+
+The core's JSON-RPC server, compiled with the crate's `server` feature. It
+binds the HTTP listener for a `CoreRuntime`, serves `POST /rpc` and the
+surrounding routes (health, schema, SSE, WebSockets, the OpenAI-compatible
+`/v1` router, OAuth callbacks, `/dev/connect`), and bridges live domain
+events onto Socket.IO for the desktop webviews. The desktop app's embedded
+core and `openhuman-core run` / `serve` both run it. Dispatch itself is the
+core's: every transport here resolves a method through
+`openhuman_core::core::invoke::invoke_method`.
+
+## How it works
+
+### Starting a server
+
+There are two ways in, and both end in `serve`:
+
+```text
+ openhuman-core run|serve desktop shell (core_process.rs)
+ install_cli_server() run_server_embedded_with_ready(
+ -> core::server_launcher host, port, socketio, cancel,
+ launch(ServeRequest) ready_tx, Some(bearer))
+ headless_api? run_server_headless |
+ : run_server |
+ \ /
+ v v
+ run_server_with_services(host, port, ServiceSet, ...)
+ HostKind: TauriShell if embedded, else detect_standalone()
+ TokenSource: Fixed(bearer) if handed in, else EnvOrFile
+ OPENHUMAN_E2E set -> ToolGroups::advertised()
+ session_store::install() (before boot: recovery)
+ CoreBuilder::new(..).build()
+ |
+ v
+ serve(&runtime, ready_tx, shutdown_token)
+```
+
+`run_server` and `run_server_embedded` use `ServiceSet::desktop()` with
+Socket.IO set by the caller; `run_server_headless` uses
+`ServiceSet::headless_api()` (request/response only, no detached background
+jobs). An embedder that built its own `CoreRuntime` can call `serve` directly.
+
+`serve` then:
+
+1. Returns right after `start_services()` if the runtime did not select
+ `ServiceSet::rpc_http` (a harness-only embedder has no transport).
+2. Resolves host and port: the builder's values, else `OPENHUMAN_CORE_HOST`
+ / `OPENHUMAN_CORE_PORT`, else `127.0.0.1:7788`.
+3. Refuses a public bind without an operator-supplied RPC token
+ (`OPENHUMAN_CORE_TOKEN` or an in-memory bearer). The generated
+ `core.token` file does not count.
+4. Picks a port with `pick_listen_port_for_host_with`. With `ready_tx` (the
+ desktop shell) another core already on the port is taken over
+ (`OccupiedByCore::Takeover`); a headless serve moves to a free port
+ instead (`Fallback`).
+5. Sets `OPENHUMAN_CORE_RPC_URL` to the bound address.
+6. Builds the router and wraps it in a layer that runs every request inside
+ `CoreContext::scope` for this runtime.
+7. Awaits `start_services()` (startup migrations finish before readiness),
+ then sends `EmbeddedReadySignal { port, fallback_from }` on `ready_tx`.
+8. Serves until the cancellation token fires (embedded) or the process
+ shutdown signal arrives, runs `exit_cleanup()` even if serving failed,
+ then returns the serve result.
+
+### The router
+
+`http/mod.rs::build_core_http_router(socketio_enabled)` registers the
+`http_host` controllers and assembles the routes. Request middleware runs
+outermost to innermost:
+
+```text
+ CoreContext::scope (added by serve)
+ socket.io layer only when socketio_enabled
+ cors_middleware preflight + CORS headers, origin allowlist
+ rpc_auth_middleware bearer on protected paths
+ request log non-/rpc requests with timing
+ route handler
+```
+
+| Route | Handler | Auth |
+| --- | --- | --- |
+| `GET /` | info page with endpoint list | public |
+| `GET /health` | granular liveness; 503 only for a critical component | public |
+| `GET /schema` | controller schema discovery | public |
+| `POST /rpc` | `rpc_handler`, 64 MiB body limit | bearer |
+| `GET /events` | SSE filtered by `client_id` | bearer header, or a one-shot bind token from `core.events_subscribe_token` |
+| `GET /events/webhooks` | webhook debug SSE | bearer header or `?token=` |
+| `GET /events/domain` | every `DomainEvent` as SSE, for the live event log | bearer |
+| `GET /ws/dictation` | dictation WebSocket | bearer header or `?token=`, plus origin check |
+| `GET /ws/live-voice` | live voice agent WebSocket | bearer header or `?token=`, plus origin check |
+| `GET /oauth/mcp/callback` | MCP browser OAuth redirect target | public; the one-time `state` is the guard |
+| `GET /dev/connect` | debug handoff to a loopback Vite renderer | public; guarded in the handler |
+| `/v1/*` | the core's OpenAI-compatible router (`inference::http`) | core bearer or the user-managed external API key |
+
+The bearer is the per-launch RPC token the core owns
+(`openhuman_core::core::auth`); `auth.rs` is only the route policy over it.
+
+### A failed call
+
+`rpc_handler` decodes the controller's `StructuredRpcError` (if any), then
+`classify.rs::classify_failure` picks a `FailureDisposition`:
+`ExpectedUserState`, `WalletNotConfigured`, `ParamValidation`,
+`SessionExpired`, `UsageProbeBackoff`, `TransientDownstream`,
+`UnknownMethod`, or `Unexpected`. Only `Unexpected` is an error-level Sentry
+event; the rest are logged at lower levels, with caller-supplied text
+redacted or sanitized where it could carry params or tokens. The JSON-RPC
+error the caller sees is the same for every class, except that
+`UsageProbeBackoff` replaces the internal marker with a client message.
+
+### Socket.IO
+
+`socketio.rs::attach_socketio` returns the layer and the `SocketIo` handle;
+`spawn_web_channel_bridge` starts the forwarders. A client authenticates by
+passing the bearer in the handshake `auth` map. Sockets that never presented
+it may receive broadcasts but cannot trigger work.
+
+Client to server events: `rpc:request` (answered with `rpc:response` or
+`rpc:error`), `chat:start`, `chat:cancel` and `thread:subscribe`. Each
+connection joins its own room and the `system` room and gets a `ready`
+event.
+
+Server to client, `spawn_web_channel_bridge` spawns one forwarding task per
+source: web-chat events (`web_chat::subscribe_web_channel_events`, sent to
+the initiating client's room and the `thread:<id>` room, not broadcast),
+dictation hotkeys and transcription results (`voice::dictation_listener`),
+overlay attention bubbles (`desktop::overlay::subscribe_attention_events`),
+core notifications (`desktop::notifications`), and companion state. It also
+forwards a set of `DomainEvent`s read off `BUS`: session expiry, MCP setup
+secret requests, memory sync and tree-build progress, channel listener
+health, and active-workspace changes. Everything except web chat is
+broadcast to every client, most under both a colon- and an
+underscore-separated name (`workspace:changed` and `workspace_changed`).
+
+`COMPANION_STATE_BUS` and `publish_companion_state_changed` are a
+transport-only seam for the shell's companion: the native macOS notch
+WKWebView has no Tauri IPC bridge and connects to this Socket.IO endpoint
+directly.
+
+## Layout
+
+| Path | What it does |
+| --- | --- |
+| `mod.rs` | Module wiring and the public re-exports. |
+| `serve.rs` | `serve` and `EmbeddedReadySignal`. |
+| `shims.rs` | `run_server`, `run_server_headless`, `run_server_embedded`, `run_server_embedded_with_ready`, plus host and port defaults. |
+| `cli.rs` | `install_cli_server` and the launcher it installs. |
+| `auth.rs` | `rpc_auth_middleware`: public paths, query-token paths, and the `/v1` external-key check. |
+| `classify.rs` | `classify_failure` and `FailureDisposition`. Pure. |
+| `socketio.rs` | Socket.IO handshake auth, client event handlers, the event bridge, the companion seam, and the event payload types. |
+| `dev_connect.rs` | `GET /dev/connect`. |
+| `testing.rs` | Test-only environment lock shared by this crate's tests. |
+| `http/mod.rs` | `build_core_http_router`, the body limit, root and 404 handlers, request log middleware. |
+| `http/rpc_handler.rs` | `POST /rpc`. |
+| `http/cors.rs` | CORS headers and preflight over the shared origin rule. |
+| `http/health.rs` | `GET /health` and `GET /schema`. |
+| `http/events.rs` | The three SSE routes. |
+| `http/dictation.rs`, `http/live_voice.rs` | WebSocket upgrade guards; the sessions themselves live in the core's `voice` domain. |
+| `http/oauth_mcp.rs` | MCP OAuth callback, handing off to `mcp::registry::oauth::complete`. |
+| `http/pages.rs` | Static HTML pages for OAuth callbacks. |
+
+## Key entry points
+
+- `install_cli_server()` (`cli.rs`): call once before `run_core_from_args`.
+ It also registers the `http_host` controllers.
+- `serve(&CoreRuntime, Option<oneshot::Sender<EmbeddedReadySignal>>,
+ Option<CancellationToken>)` (`serve.rs`).
+- `run_server_embedded_with_ready(..., rpc_token)` (`shims.rs`): pass the
+ bearer the shell already holds so the server never reads it from the
+ environment.
+- `build_core_http_router(socketio_enabled)` and `rpc_handler`
+ (`http/`): for tests that want the router without a listener.
+- `publish_companion_state_changed(payload)` (`socketio.rs`).
+
+## Boundaries
+
+- Controllers, params, errors and session-expiry semantics are the core's.
+ Never add method-name branches here; register controllers in
+ `openhuman-core/src/core/all.rs` (or as an extension, as `http_host` does).
+- The `/v1` inference router, the dictation and live-voice sessions, and the
+ MCP OAuth completion are domain code in the core, mounted here behind the
+ core's `http-server` gate.
+- The token is minted and verified by `openhuman_core::core::auth`; listener
+ port selection is `openhuman_core::platform::connectivity::rpc`.
+- The event payload types (`WebChannelEvent` and friends) are constructed by
+ core domains; this module only transports them.
+
+## Gotchas
+
+- `/dev/connect` only exists in debug builds, or in release with
+ `OPENHUMAN_DEV_CONNECT=1`. It accepts only a bare `http://` loopback `app`
+ origin, requires a loopback `Host`, refuses cross-site navigations, and
+ puts the bearer in the URL fragment with `no-store` and
+ `Referrer-Policy: no-referrer`.
+- `/events` is exempt from the middleware's header check but not
+ unauthenticated: the handler requires a bearer or a bind token, which is
+ consumed on use so a leaked URL cannot be replayed.
+- The Socket.IO origin check is wider than the CORS one (any scheme on a
+ loopback or `tauri.localhost` host). Both read
+ `OPENHUMAN_CORE_ALLOWED_ORIGINS`.
+- `OPENHUMAN_E2E` switches tool groups to `ToolGroups::advertised()` in the
+ `run_server*` shims so the browser E2E mock model can call tools. Library
+ hosts are unaffected.
+
+## Tests
+
+Sibling `*_tests.rs` files cover auth route policy, failure classification,
+the shims, Socket.IO, `/dev/connect`, and each route module under `http/`
+(including the `/rpc` handler's Sentry routing and the `/v1` mount in
+`inference_route_tests.rs`).
+
+```bash
+cargo test -p openhuman-rpc --features crash-reporting server::
+```