# Web UI port

- **Default**: `18100`, or the next free port from it, chosen once per account and kept
- **Serves**: FastAPI `/api/*`, the OpenAI-compatible `/v1/*`, `/ws`, `/healthz`, the static web UI export, to browser, TUI and CLI alike. [Single-port architecture](https://fzkuji.github.io/OpenProgram/docs/reference/design/cli/single-port.html)

## Setting it

Highest first:

| Source | Scope |
| --- | --- |
| `openprogram web --web-port <p>` | That run, not persisted; `openprogram web` only |
| `OPENPROGRAM_WEB_PORT` | That process, not persisted |
| `ui.web_port` in `~/.openprogram/config.json` | Persisted, read at the next `openprogram web`, `openprogram worker` or App start. Nothing live is rebound |
| `~/.openprogram/web-port` | The port the account chose for itself at its first start: the lowest free port from `18100` up. Kept on this machine, not in backups |

| Command | Effect |
| --- | --- |
| `openprogram ports` | Shows the current port |
| `openprogram ports --port 9100` | Sets and persists |
| `openprogram setup ui` | Wizard: port (`1–65535`) and auto-open-browser |

The worker is the only thing that chooses: `claim_worker_port()` in `operations/worker/lifecycle.py` settles the port right after the worker takes its lock, and `resolve_worker_port()` reads it. The App reads the same files (`apps/desktop/main/worker/web-port.js`), so the two always agree.

## One port per account

Loopback ports belong to the whole computer. With one fixed port, a second account signed in on the same PC, or anyone with another program already on 18100, had no worker and an App that waited forever. Each account therefore owns its port:

1. The first start of an account's worker takes the lowest port from 18100 up that it can bind, and writes it to `web-port`. The first writer wins, so a first App start and a first CLI start agree.
2. Later starts use the recorded port, so the address stays the same.
3. When something else holds the recorded port at start, the worker moves to the next free one and rewrites the record. The App sees the record change and opens again on the new port.
4. A port the user set (`ui.web_port`, `OPENPROGRAM_WEB_PORT`) never moves. Its holder is named and the worker exits.

Jupyter works the same way: it takes the next free port and writes the address it serves to a runtime file the clients read.

## When the port is taken

| The port is | The worker | The App | `openprogram web` |
| --- | --- | --- | --- |
| Free | Binds, starts | Opens the chat signed in | Starts the worker, opens the browser signed in |
| Held by this account's worker | The worker lock already prevents a second worker | Opens the chat signed in | Points the browser at the UI |
| Held by another program or account, port chosen | Names the holder, moves to the next free port, rewrites `web-port` | Starts this account's worker, then opens again on the new port | Starts the worker and opens the port it names |
| Held by another program or account, port set by the user | Names the holder and exits; the version is not blamed for it | Shows *Port N is in use* with the holder and how to choose another port, and loads the chat once the port is freed | Refuses, names the holder |
| Recently exited (TIME_WAIT) | Counts as free: POSIX probes with `SO_REUSEADDR`; Windows rebinds such a port anyway | Counts as free: POSIX probes with `SO_REUSEADDR`; Windows rebinds such a port anyway | Counts as free: POSIX probes with `SO_REUSEADDR`; Windows rebinds such a port anyway |

Never kills the holder, and never gives a listener this account's token unless it proves it is this account's worker.

| Check in operations/worker/ports.py | How |
| --- | --- |
| Liveness: `port_in_use(port)` | Bare TCP connect |
| Free to listen: `port_bindable(port)` | A trial bind. On Windows with `SO_EXCLUSIVEADDRUSE`: `SO_REUSEADDR` there would bind over a live listener and call a held port free |
| Identity: `backend_is_ours(port)` | Needs this profile's live worker (its lock) and its `worker.port`, then the profile's owner-only `web/access.json`; sends a random nonce to `/api/auth/challenge` and verifies the token-HMAC proof locally. Never sends the owner token or a Bearer header, so a foreign listener cannot obtain the credential. Optional `expected_revision` binds the proof to an upgrade target's revision. |
| Ownership: `describe_port_owner(port)`, `port_owner_hint(port)` | `lsof` or `netstat` + `/proc`, `ps`, Windows CIM: PID and command line |

`openprogram web auth-url` exits with code 3 and names the holder when this account has no worker but something listens on the asked port; the App shows that holder instead of waiting.

**Why 18100**

- Below 49152, so never in the OS ephemeral range handed to outbound sockets.
- The `18xxx` block is rarely used by dev tooling, unlike 3000, 5173, 8000, 8080. The development instance's 18200 is outside the 18100–18199 range accounts choose from.
- Kept once chosen: a bookmarkable URL, and browser state (localStorage, service worker scope) that survives restarts.

**Compared with OpenClaw**

OpenClaw pins its gateway to `18789` and refuses when it is taken; OpenProgram keeps that for a port the user set and moves a port it chose itself.

| OpenClaw layer | Source | OpenProgram |
| --- | --- | --- |
| Single-instance lock (pid, start time, argv) | `src/infra/gateway-lock.ts` | `worker.lock` (fcntl), `worker.pid` with start time, `_process_alive` |
| EADDRINUSE retry for TIME_WAIT | `src/gateway/server/http-listen.ts` | `SO_REUSEADDR` on POSIX, no retry loop |
| Names the holder via `lsof` | `src/infra/ports.ts`, SSH-tunnel path only | `describe_port_owner`, `port_owner_hint`, in every "port in use" message on the start path and on the App's page |
