# Single-port architecture

| Path (in match order) | Served by the worker from | Cache |
| --- | --- | --- |
| `/ws` | Native WebSocket |  |
| `/api/*` | Native routers |  |
| `/v1/*` | OpenAI-compatible routers: [OpenAI-compatible endpoint](https://fzkuji.github.io/OpenProgram/docs/reference/design/integrations/openai-compatible-endpoint.html) |  |
| `/files/raw`, `/docs/*` | Static mounts |  |
| `/_next/static` | `apps/web/out/` | Immutable |
| `/*` | Files in `out/`; any other GET gets the shell `out/chat.html` | No-cache for HTML |

The Electron shell loads the same origin. Ports and conflicts: [Web UI port](https://fzkuji.github.io/OpenProgram/docs/reference/design/cli/ports.html). Stable: `18100`, or the next free port the account chose at its first start; development `18200`.

## Invariants

- **The backend is the sole origin.** Nothing proxies it and nothing bakes a port into the build. `/ws` is right by construction: it is the origin the page loaded from.
- **API routes beat static.** The frontend mount registers last.
- **Node is build-time only.** Runtime needs Python and the worker.

## Why not a Next server

| A Next server beside the worker would cost | Here |
| --- | --- |
| The `/ws` proxy target fixed at build time; a build with the wrong profile env leaves every client disconnected | Relative `/ws`, `/api/...`; no `rewrites()` |
| Worker code for port reclaim, orphan killing, build watching, parent-PID watch | Worker starts no Node process |
| Node at runtime for a UI that is all client-side | Static export |

Merging is cheap: the shell loads through `next/dynamic` with `ssr: false`, every real page is `"use client"`, and there is no `middleware.ts`, server action or `next/image`. FastAPI has `docs_url=None`, so nothing collides with the docs mount.

## Frontend export

| Part | Design |
| --- | --- |
| `apps/web/next.config.mjs` | `output: "export"`: `next build` writes HTML, JS and CSS into `apps/web/out/` |
| Dynamic segments | Session, skill and provider-settings paths have no page files; static export would reject them without `generateStaticParams`. The SPA fallback serves the shell and the client routes from `pathname`. A segment that does real work keeps `generateStaticParams` with one placeholder. |
| API route handlers | None; the worker serves those paths |
| Serving | `openprogram/interfaces/server/frontend.py`, mounted last in `create_app()` |
| Build gate | `ensure_frontend_built`: if `out/index.html` is missing or older than the newest source, run `npm run build` once at startup. A packaged release ships `out/` and never invokes Node. |

## Process and port

| Part | Design |
| --- | --- |
| Entry | `operations/worker/runner.py` calls `interfaces.server.start_web` and `ensure_frontend_built` |
| Discovery | The `worker.port` file is the single source of truth |
| Port knobs | `OPENPROGRAM_WEB_PORT` and `ui.web_port` set the one port |
| Electron | `apps/desktop/main.js`: `WEB_PORT` is `OPENPROGRAM_WEB_PORT`, else the instance's port. Start URL, origin check and navigation guard use it |

`operations/worker/web.py` is leftover dual-port code outside the active call path, retained for a separate cleanup. It still names a watcher script that no longer exists.

## Trade-offs

- No `next dev` hot reload against the merged origin.
- A dynamic segment that really rendered content would lose its deep link once its page file is gone. The SPA fallback covers it.
- `out/` can go stale after a pull. The startup gate covers it; run `openprogram restart` after `git pull`.

**Acceptance criteria**

1. `openprogram` (dev profile) starts exactly one listening port; `lsof` shows no `next-server`.
2. Fresh loads on `/chat`, `/s/<id>`, `/settings/providers/<id>` and `/skills/<name>` render; `/ws` connects; `/api/pick-folder` works.
3. Killing the worker leaves a loaded page showing disconnected; restarting reconnects. No orphan processes.
4. A build with no profile env produces a working instance.
5. Tests pass; desktop app repackaged and verified.

## Distribution

| Install | Python |
| --- | --- |
| macOS desktop | CPython and core dependencies embedded in the signed bundle; nothing downloaded at first launch |
| CLI, server | uv-managed environment |
| Source checkout | Development build flow |

Authority: [Installation, packaging, release, and upgrade](https://fzkuji.github.io/OpenProgram/docs/reference/design/distribution/installation-packaging.html).

## Implementation status

Implemented in the current runner and frontend mount. Desktop supervision and distribution status live in the page linked above.
