diff --git a/doc/2026-11-01_design_id2146.md b/doc/2026-11-01_design_id2146.md new file mode 100644 index 0000000000..4edef2c560 --- /dev/null +++ b/doc/2026-11-01_design_id2146.md @@ -0,0 +1,120 @@ +# Design: programs as websites in `id serve` + +- Date: 2026-11-01 +- Status: **design** — this note recommends the serving contract for #2146; it is not implemented. +- Issue: [#2146 — how does a program appear as a website?](https://github.com/developing-today/code/issues/2146) +- Scope: world-backed program surfaces, their listener/path placement, authentication, session authority, and presentation. Runtime publish/unpublish mechanics remain in #2147; the listener inventory and legacy binding behavior remain in #2145. + +## Decision to make + +Define what a program website is, how a `serve` process exposes it on a program-only listener, alongside Explorer on a separate listener, or at a path on the Explorer listener, and what security and session context apply. The contract must fit the existing `WorldHub`, web security, and world session models without allowing guest programs to inject arbitrary HTML. + +## Context and evidence + +The issue records the product gap and asks for decisions on target identity, listener/mount authentication, the relationship to world hosting, and per-session identity/capabilities/rendering. Today `id serve --web` has one shared `--bind`/`--port` listener (`pkgs/id/src/cli.rs:274-285,345-357`) and starts one router on it (`pkgs/id/src/commands/serve.rs:1130-1145,1221-1285`). That router contains the Explorer routes and the optional `/world` page plus `/ws/world` bridge (`pkgs/id/src/web/routes.rs:257-319`). + +The hosted unit is already a world: `--world` enables hosting, `--world-module` selects a Wasm module for the default world, and `--world-name` names the offered world (`pkgs/id/src/cli.rs:364-372,397-402`). Startup creates a shared `WorldHub` for the Iroh protocol and web bridge and opens the default world (`pkgs/id/src/commands/serve.rs:887-931`). The current browser page is a lobby: it asks for a display name and guest capability, and offers a separate host-admin token field for inviting/installing (`pkgs/id/src/web/templates.rs:785-823`). The browser then sends a `join` frame with the capability and selected world (`pkgs/id/web/src/world.ts:288-315`). + +These credentials are not interchangeable. The web token is request-wide `WebSecurity` on the web router (`pkgs/id/src/web/security.rs:39-72`); the world session protocol authorizes by guest capability alone, not transport identity (`pkgs/id/src/world_session/mod.rs:9-15`). The world admin secret authorizes capability minting (`pkgs/id/src/world_session/mod.rs:418-460`). A web token must not become a world capability or admin credential. + +The newest local design record says guests run on the host and trusted client adapters render their output; current output is legacy text or a portable screen, with guest HTML/CSS explicitly excluded (`doc/2026-10-31T17-47-20Z_design_world_presentation_and_fuel/2026-10-31T17-47-20Z_design_world_presentation_and_fuel.md:11-18,29-38`). It calls web-native specialization an open question for #2146 and notes that no DOM/HTML capability exists (`doc/2026-10-31T17-47-20Z_design_world_presentation_and_fuel/2026-10-31T17-47-20Z_design_world_presentation_and_fuel.md:86-100`). + +**Companion decision alignment.** The Nov 1 triage record on [#2145](https://github.com/developing-today/code/issues/2145) says the bindings work keeps the same Explorer router on each listener for that issue, while the binding type must leave room for a future content target; its v1 token model applies the global `--web-token` to every listener. It also clarifies that `defaults.conf` is embedded build-time configuration, not a runtime per-user serve config. This note adds the content-target model without treating those planned binding changes as already implemented. The Nov 1 triage record on [#2147](https://github.com/developing-today/code/issues/2147) keeps runtime publication/persistence in that issue, blocked on #2145 and #2146; its proposed canonical paths and explicit acknowledgement for Explorer-route overrides are treated below as the follow-on contract, not current behavior. + +## Viable options + +### 1. What is a publishable program? + +| Option | Benefits | Costs / risks | +| --- | --- | --- | +| **A. A world-backed program (recommended)** | Uses the existing `WorldHub`, named-world selection, session protocol, capability ceiling, and trusted presentation adapter. A program site is a different web front door to a hosted world, not a second runtime. | Does not make arbitrary documents or node data into websites. Multi-program-per-world selection is not designed here. | +| B. Any addressable program/document in the node | Broadly general and could unify pages, blobs, and programs. | No existing common addressable-program abstraction or rendering/security contract; would conflate static documents with stateful world sessions and expand this issue into a new content model. | +| C. A web-native guest page (HTML/CSS/JS) | Familiar authoring model and full browser layout control. | Introduces guest-controlled active content, injection/origin risks, and a second presentation/runtime contract. It conflicts with the latest presentation decision that guests emit text or inert screen data, not HTML/CSS. | + +**Recommendation:** v1 publishes a world by its existing `WorldName`; the active guest program in that world supplies the behavior. A publication is not a raw file/blob, a document route, or arbitrary HTML. Do not add a new program ID until the runtime supports multiple independently addressable programs in one world. The visible output uses the existing trusted text/screen web presenter; no guest HTML, CSS, script, or arbitrary URL fetches. + +**Selector semantics:** use the existing world name (the `--world-name` selector, default `lobby`) as the program target; express the binding's content target conceptually as `program:`. `--world-module` continues to choose the module installed in the default world; it is not a separate public program ID. A mount path chooses where the target appears, not which program it is. In a multi-listener setup, associate each surface with a stable binding identity, never by the order of repeated flags. The #2145 triage currently describes bindings by socket address, so #2146 implementation must add/settle a stable name if that representation cannot uniquely address bindings (notably port-0 bindings). Keep config in the #2145 embedded build-time defaults model; do not imply a new runtime per-user config file. + +### 2. How do listeners and mounts compose? + +These are composable placements of the same target, not competing runtime models: + +| Placement | Proposed behavior | Trade-off | +| --- | --- | --- | +| **Program-only listener** | A listener's content target is `Program(WorldName)` and the program frontend owns `/`; Explorer routes are absent from that listener. | Clean origin/port separation; consumes another port and still shares the `serve` process and world hub. | +| **Mixed ports, one process** | Separate bindings select `Explorer` or `Program(WorldName)`. The process shares the store and hub but each listener gets a router for its declared surface. | Keeps operational state in one process while allowing different origins/ports; requires listener-specific router construction, which is beyond #2145's initial same-router behavior. | +| **Path mount** | A program target mounts at an absolute canonical path on an Explorer listener; all its page, asset, API, and WebSocket paths are rooted beneath that mount. Prefix matching is on segment boundaries (`/app/x` matches `/app`; `/application` does not). | Avoids extra ports, but introduces route precedence and base-path complexity. Explorer routes must not be shadowed by accident. | + +Model content as part of a binding's surface, conceptually `Explorer` or `Program(WorldName)`, with an optional mount path. CLI/config syntax can vary, but it must express that target selector and associate it with a stable binding identity without positional flag pairing. Preserve the legacy `--web` behavior as an Explorer binding when no content target is specified. + +For path mounts, canonicalize once, reject invalid or ambiguous paths, and reserve existing Explorer paths by default. A conflicting publication fails; an intentional Explorer-route override requires an explicit acknowledgement and a loud report, consistent with #2147's newest triage proposal. Do not silently resolve route collisions by order. Mount-scoped assets and `/ws/world` must not escape to the listener root or collide with another mount. + +### 3. Authentication and public-by-default + +| Option | Benefits | Costs / risks | +| --- | --- | --- | +| **A. Inherit the existing listener security and require a world capability for a world session (recommended v1)** | Reuses existing `--web-token` behavior and the world authorization boundary; avoids secret pairing/precedence rules across bindings. | Cannot give Explorer and a program on different listeners different web tokens in v1; path mounts cannot override listener security. | +| B. Independent auth policy/token per listener and per mount | Supports a private Explorer with a public program mount, or distinct trust zones on separate ports. | Adds token/config lifecycle, precedence, and routing complexity; this is a deliberate extension beyond #2145's recorded global-token v1. | +| C. Anonymous world sessions by default | Makes a published program immediately browsable without an invitation. | Silently grants session access and needs a new anonymous capability-minting/permission policy; incompatible with treating the current guest capability as the world authorization boundary. | + +**Recommendation:** for v1, every listener inherits the process-wide `--web-token` policy, and every path mount inherits its listener's policy. A mount cannot weaken that policy or silently mint anonymous world-session authority. Keep the existing default bind on loopback (`pkgs/id/src/cli.rs:345-357`). A tokenless non-loopback listener remains externally reachable at the HTTP layer with the existing startup warning rather than becoming a new hard refusal (`pkgs/id/src/commands/serve.rs:1269-1273`); that warning is not an authentication guarantee. Regardless of HTTP access, a browser must present a world guest capability to create a world session. Neither `--web-token` nor a transport identity grants world authority. + +This keeps two layers distinct: the listener token governs access to the web router; the guest capability governs a program session and its world scopes. The `--world-admin-token` remains host-only authority for minting invites and is never passed to the guest. Mount-specific web tokens and anonymous capabilities are not v1 behavior. + +### 4. What does the program receive and render? + +The program runs in the host's world runtime. Each browser participant gets a normal world session, authorized by its own guest capability and bounded by the world's configured capability ceiling. The session has the participant/display name and protocol inputs; it does not inherit the HTTP bearer token, host admin token, host filesystem, request headers, or an Iroh identity. Treat a browser as a guest transport, not as an authenticated person unless a separate identity feature is designed later. + +Rendering is the trusted adapter for the existing presentation contract: legacy UTF-8 text or the versioned portable screen. The screen adapter creates inert DOM cells (`pkgs/id/web/src/world-surface.ts:343-375`); a test asserts guest markup remains a text node rather than HTML (`pkgs/id/web/src/world-surface.test.ts:8-18`). This preserves the host-authoritative computation and rendering boundary documented in the newest presentation design (`doc/2026-10-31T17-47-20Z_design_world_presentation_and_fuel/2026-10-31T17-47-20Z_design_world_presentation_and_fuel.md:11-18,29-38,86-100`). + +## Recommendation + +Adopt a **world-backed program surface** as a content target on the bindings model. Bindings choose `Explorer` or `Program(WorldName)`; a program-only listener serves the program frontend at `/` and omits Explorer, separate listeners in one `serve` process may select different surfaces, and a path mount places the same frontend under a canonical prefix on an Explorer listener. The surface is a trusted adapter over the host-run world session—not a new guest web runtime. + +Use existing world names for target identity, guest capabilities for program authority, and the global web token only for listener-level HTTP access. V1 routes inherit listener auth; no route silently receives anonymous world-session authority, and there are no per-mount or per-listener token overrides. Keep dynamic publication, persistence, CLI/API commands, and route-override acknowledgement mechanics in #2147, which consumes the target/path contract decided here. + +## Invariants + +1. **One target vocabulary:** v1 program targets are existing `WorldName`s; file, blob, and generic document targets are out of scope. +2. **Content isolation:** a `Program(WorldName)` listener does not serve Explorer pages/APIs; a path mount serves only within its canonical segment prefix. +3. **No implicit shadowing:** reserved Explorer-route collisions fail unless a later explicit override operation acknowledges the shadow and reports it. +4. **One host runtime:** web sessions reach the same host-side `WorldHub` and capability ceiling as other world sessions; a second process/runtime is not introduced. +5. **Authority separation:** web token, guest capability, and world admin token remain distinct; only the guest capability authorizes world actions. +6. **Least authority:** a website visitor is a world guest with the capabilities granted to that guest, not a server admin or implicit public principal. Tokenless non-loopback HTTP exposure does not itself mint or grant a world capability. +7. **Safe rendering:** guest output remains text/portable-screen data rendered by trusted client code; never interpret guest output as HTML/CSS/JS. +8. **Compatibility:** with no content target, the current `--web` Explorer behavior and `/world` lobby remain unchanged. +9. **Config honesty:** do not describe the embedded `defaults.conf` mechanism as a runtime user config. Align with #2145's recorded build-time-default semantics; runtime publish/unpublish configuration belongs to #2147. + +## Acceptance criteria for implementation + +- Existing `id serve --web` continues to serve Explorer, and existing `/world` plus `/ws/world` behavior remains available on Explorer listeners. +- A configured program-only binding serves the selected world program at its root and does not expose Explorer routes or APIs on that listener. +- Two listeners in one process can independently select Explorer and a program surface and both answer requests; listener bind failure remains loud and does not silently produce a partial topology (the binding invariant is owned by #2145). +- A mounted program works at its base path and nested paths, including its assets and WebSocket; requests outside its segment prefix do not reach it. Invalid mounts and reserved-route conflicts fail clearly; no conflict is resolved silently. +- Web-token checks apply consistently to all listeners and mounts; a mount cannot weaken them. The default bind remains loopback. A tokenless non-loopback bind emits the existing warning. +- No program session is established without a valid world guest capability; the web token and world admin token do not satisfy that requirement. A browser session receives only guest-scoped authority. Anonymous visitors cannot view session output unless a separate explicit public-capability mode is approved and implemented. +- Tests prove guest-produced markup is rendered as inert data, not interpreted as HTML, and that the browser presenter handles the supported text/screen output. +- Tests cover cross-mount isolation, route-conflict rejection/acknowledgement, target resolution by world name, and an unauthorized/authorized session round-trip. + +## Phased implementation plan + +Each phase is a separately reviewable, mergeable slice with compatibility tests. The bindings foundation must land first; #2145's recorded implementation packet owns listener inventory/config/token basics. + +1. **Surface contract and compatibility layer.** After the #2145 binding API settles, add the content-target type and validation (`Explorer` / `Program(WorldName)`), defaulting every existing binding to Explorer. Ensure content metadata attaches by stable binding identity (settling a name if the #2145 API has only addresses); do not pair repeated flags by order. Test legacy startup and target resolution; do not change routing yet. +2. **Program-only surface.** Build the world-program frontend against the existing trusted presenter and guest-session protocol. Attach it to a program-only listener at `/`, omit Explorer routes, and test the capability gate plus text/screen rendering. Keep `/world` on Explorer unchanged. +3. **Mixed listeners.** Construct each listener's router from its declared surface so one process can serve Explorer and one or more program targets on separate ports. Cover listener enumeration, a full browser-session round-trip on each surface, and startup failure without a partial listener set. +4. **Path mounts.** Add canonical mount paths, segment-boundary dispatch, scoped page/assets/API/WebSocket URLs, and explicit route-conflict rejection. Add an acknowledged override only through the #2147 publication operation; test collisions and mount isolation before enabling that operation. +5. **#2147 publication integration.** Have runtime publish/unpublish address a settled binding identity, canonical path, and world target; persist/restore that declarative target set as #2147 specifies. Keep publication lifecycle and persistence out of the #2146 implementation itself. + +## Question for Drew + +- **Anonymous public program sessions:** should a later explicit public mode mint anonymous, least-privilege guest sessions for a published program, or should every browser visitor always need an invite capability? **Recommendation:** keep v1 invite-capability gated as above; consider explicit public mode only with a configured capability ceiling and rate/session limits. This does not block the serving model or its first implementation slice. + +## References + +- Issue #2146: [program-as-website design request](https://github.com/developing-today/code/issues/2146). +- Issue #2145: [bindings model and Nov 1 triage record](https://github.com/developing-today/code/issues/2145). +- Issue #2147: [runtime publish/unpublish and Nov 1 triage record](https://github.com/developing-today/code/issues/2147). +- Current CLI/listener/router: `pkgs/id/src/cli.rs:274-285,345-372,397-402`; `pkgs/id/src/commands/serve.rs:887-931,1130-1145,1219-1285`; `pkgs/id/src/web/routes.rs:257-319`. +- Current auth/session boundary: `pkgs/id/src/web/security.rs:39-72`; `pkgs/id/src/world_session/mod.rs:9-15,418-460`. +- Current world presenter decision: `doc/2026-10-31T17-47-20Z_design_world_presentation_and_fuel/2026-10-31T17-47-20Z_design_world_presentation_and_fuel.md:11-18,29-38,86-100`.