Skip to content

The router routes every other request to one cell by host, SCIM, issuer or the first cell - #523

Merged
zlareb1 merged 1 commit into
mainfrom
feat/cell-router-routing
Nov 12, 2026
Merged

zlareb1 merged 1 commit into
mainfrom
feat/cell-router-routing

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

Changelog category (leave one):

  • New feature

Changelog entry (one or two sentences a user of Loom can read without the diff; the release pull request collects these into the release notes):

loom router serve now routes every request that is not its own to one cell: a cell's own host to that cell, SCIM on the shared host to the first cell, a token-bearing request on the shared host to the cell that accepts its issuer, and everything else to the first cell (or, before one is registered, to LOOM_ROUTER_UPSTREAM_URL). Requests and answers are streamed through, with the client's host, scheme and address in X-Forwarded-*.

Why

The ninth of the pull requests that implement cells. It stacks on #522. Until now the router answered only its own paths; the page's rules 1 to 4 are what make one URL serve many cells.

Design

  • The decision is pure (server/router/routing.py): build makes a Table from the directory's cells (name, host, state, first mark, issuers, upstream address) and hosts aliases; decide reads the host, path, Authorization and, for POST /oauth/revoke, the revoked token:
    • rule 1: a host that names a cell (normalised as the directory writes hosts) goes to it; a host that is neither a cell's nor the shared host answers 421;
    • the shared host is LOOM_ROUTER_SHARED_HOST, the first cell's own host and its hosts aliases (the setting's description now says so);
    • until the first cell is serving, everything on the shared host goes to rule 4's cell, which verifies it;
    • rule 2: SCIM on the shared host goes to the first cell, except /scim/v2/Groups with a JWT, which goes by its issuer;
    • rule 3: a bearer JWT's iss, read unverified, picks the cell that lists it; one no cell lists answers 401 with WWW-Authenticate: Bearer error="invalid_token", as a cell's own 401 carries a challenge; a bearer that is no JWT names no issuer;
    • rule 4: everything else goes to the first cell; a revocation goes by the revoked token's issuer, an opaque refresh token to the first cell;
    • a cell with no upstream address yet answers 503, the first cell's included once it is registered (the pass-through is only for before then);
    • an issuer two cells list (which attach refuses) routes to neither, logged; the directory read is ordered so every replica builds the same table;
    • a token payload nested past the JSON parser's depth names no issuer rather than a 500.
  • Where a cell is reached: a new directory column, cells.upstream_url (schemas/router_migrations/002_cell_upstream.sql), an address inside the deployment's network, since a cell's public host names the router itself. Attach writes it (attach is not in the stack yet); LOOM_ROUTER_UPSTREAM_URL is rule 4's cell before the first cell is registered. The page now says this, in a paragraph after the rules.
  • The forward (server/router/app.py, Router.forward, proxy_client): one shared httpx.AsyncClient with a 5 s connect timeout, no read timeout (an event stream may idle) and no bound on open connections, so a request never waits for a pool slot (PROXY_TIMEOUT, PROXY_LIMITS); the request goes on as the client sent it (raw path and query, its own Host) with the router's own X-Forwarded-For, -Proto and -Host replacing what the client claims (and Forwarded dropped), minus the hop-by-hop headers and those Connection names; every header line of the answer is kept (a sign-in's two Set-Cookie lines); both bodies stream, and a request that sent no body is forwarded with none framed; an unreachable cell answers 502, and a raw path httpx cannot carry answers 400. A revocation's body is read only on the shared host once the first cell serves, bounded at 64 KiB (413 past it, the same bounded read the placement body uses), and its last token, blank ones kept, is used, as the cell's own form reading takes it. The router's own paths come first, /livez and /readyz answer 405 for any other method, and a path under /cells/ it does not serve answers 404 rather than being forwarded.
  • The table is read once per LOOM_ROUTER_DIRECTORY_CACHE_S. Once there is one, a stale table is answered at once and read again in the background, so a directory that hangs delays no request; the read is stamped when it ends, failed or not, and a failed read keeps the last table. Before the first read every request answers 503.
  • CLI: loom router serve's help says it routes every other request to a cell (the CLI reference and snapshots regenerated).
  • Settings: LOOM_ROUTER_SHARED_HOST and LOOM_ROUTER_UPSTREAM_URL (catalog, generated reference, schema and example regenerated). loom router serve refuses an upstream that is not http(s)://host[:port] with no path, and a directory upstream_url of another shape is treated as missing (503), logged.
  • The background table read is cancelled and awaited when the router shuts down.

Scope and risk

Router only; nothing changes unless loom router serve runs in front of a deployment. Still to come: the signed snapshot of the last table on shared storage (so a replica that restarts during a directory outage still routes), and the first cell's org-first sign-in page.

Compatibility, rollout and rollback

A new column in the router's own directory, NOT NULL DEFAULT '', additive.

Operator step: each cell behind the router lists the router's addresses in LOOM_TRUSTED_PROXIES. Otherwise the cell reads every client as the router's address (one abusive client then throttles everyone's sign-in) and the scheme as the router's; the page says so. A router build without routing answers only its own paths, as before.

Tests

  • tests/unit/test_router_routing.py (43): a cell's own host (any case or default port) goes to it; an unknown host answers 421; the shared host and an alias go to the first cell; a JWT goes by its issuer and an unknown issuer answers 401; a bearer that is no JWT names no issuer; SCIM on the shared host goes to the first cell even with a JWT, but /scim/v2/Groups with a JWT goes by issuer; a revocation goes by the revoked JWT's issuer, an opaque one to the first cell; before the first cell serves, everything goes to rule 4's cell or the pass-through; a cell with no upstream answers 503. Through the app: a request reaches the cell whole (method, path, query, body, Host, X-Forwarded-*), its answer streams back with the cell's status and without hop-by-hop headers; a revocation is forwarded with the body it was routed by; /cells/ paths are never forwarded; an unreachable cell answers 502; the table is read once a window and kept through an outage; no table yet answers 503; two Set-Cookie lines both reach the client and a header the cell's Connection names does not; the raw path and query (%3F, %2F) reach the cell unchanged; spoofed X-Forwarded-*, Forwarded and a Connection-named header are replaced or dropped; a revocation goes by its last token, past 64 KiB on the shared host answers 413 unforwarded, and at a cell's own host streams on unread; POST /livez and /readyz answer 405 unforwarded; a hung directory read delays none of three requests; the forward's pool has no bound; an issuer two cells list answers 401; an upstream that is no bare http(s) address (no scheme, bad bracket, a path, another scheme, userinfo, a query) answers 503, and loom router serve refuses such a pass-through; a request with no body is forwarded with none framed; a revocation whose last token is blank goes to the first cell; an issuer no cell lists answers 401 with a Bearer challenge, unforwarded; a registered first cell with no upstream answers 503; a payload nested 5000 deep names no issuer.
  • tests/integration/test_router_directory_pg.py: directory.routing() reads each cell with its upstream_url (empty until attach). Passed against a local Postgres.
  • Removing the SCIM rule, keeping the hop-by-hop headers, collapsing repeated answer headers, waiting on the stale table's read, and framing a body that was not sent each made a test fail.
  • At this head: the router, routing, CLI-snapshot and doc tests (330 passed); the routing tests share one event loop and run the router's lifespan, as serve does; make lint and make typecheck pass. The full unit suite has not run on this stack.

AI assistance in the code (leave one; writing this description with AI is fine):

  • AI-generated (an agent wrote most of the diff): Claude Code.

  • I can explain every line of this diff and how it interacts with the rest of Loom.

Checklist

  • make format-check, make lint, make typecheck and make test-unit pass (the full unit suite result follows in Tests).
  • Generated files are regenerated in this PR, not by hand: the SDK op tables, docs/reference/configuration.md and the docs/reference/ tables (python scripts/codegen/docs_reference.py), the reach manifest, schemas/schema.sql (make schema-dump) and the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py).
  • A new migration is additive and numbered after the latest on main; no merged migration is edited.
  • A behaviour change to any file a plugin ships (hooks/, lib/, bin/, skills/, .mcp.json, the Codex plugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.
  • The frontend builds (npm --prefix frontend run build) and its tests pass, if frontend/ changed.
  • Docs under docs/ describe the system as it now is, and this description names any operator steps and wire changes.
  • Repository text has no dates, people, host names, internal namespaces, ticket or decision numbers; the history goes in this description (tests/unit/test_public_copy.py).
  • Commits are signed off (git commit -s), as CONTRIBUTING.md asks.

🤖 Generated with Claude Code

@zlareb1
zlareb1 force-pushed the feat/cell-login-org branch from 452ee5c to 9ce65fe Compare November 11, 2026 14:48
@zlareb1
zlareb1 force-pushed the feat/cell-router-routing branch from 069cab7 to 10a4863 Compare November 11, 2026 14:48
@zlareb1
zlareb1 force-pushed the feat/cell-login-org branch from 9ce65fe to 7d23c72 Compare November 11, 2026 15:11
@zlareb1
zlareb1 force-pushed the feat/cell-router-routing branch from 10a4863 to 14bdc3f Compare November 11, 2026 15:11
@zlareb1
zlareb1 force-pushed the feat/cell-login-org branch from 7d23c72 to 649c4e8 Compare November 11, 2026 15:31
@zlareb1
zlareb1 force-pushed the feat/cell-router-routing branch from 14bdc3f to 35acd3c Compare November 11, 2026 15:31
@zlareb1
zlareb1 force-pushed the feat/cell-login-org branch from 649c4e8 to 6002130 Compare November 11, 2026 15:47
@zlareb1
zlareb1 force-pushed the feat/cell-router-routing branch from 35acd3c to c93c4b7 Compare November 11, 2026 15:47
@zlareb1
zlareb1 force-pushed the feat/cell-login-org branch from 6002130 to b5f0cad Compare November 12, 2026 01:40
@zlareb1
zlareb1 changed the base branch from feat/cell-login-org to main November 12, 2026 01:40
…er or the first cell, and forwards it streamed

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/cell-router-routing branch from c93c4b7 to 988b5e8 Compare November 12, 2026 01:55
@zlareb1
zlareb1 marked this pull request as ready for review November 12, 2026 01:55
@zlareb1
zlareb1 merged commit bea3cc5 into main Nov 12, 2026
@zlareb1
zlareb1 deleted the feat/cell-router-routing branch November 12, 2026 01:55
@zlareb1 zlareb1 mentioned this pull request Nov 12, 2026
3 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant