Skip to content

The router's directory and placement API: a cell places a new org, and a client finds an org's cell - #497

Merged
zlareb1 merged 1 commit into
mainfrom
feat/cell-router-directory
Nov 11, 2026
Merged

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

Conversation

@zlareb1

@zlareb1 zlareb1 commented Nov 10, 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 runs the router's placement API and org lookup from its own Postgres directory (LOOM_ROUTER_PG_DSN, loom router migrate). A cell places a new org through it, and a client finds an org's cell with GET /cells/lookup.

Why

The fourth of the pull requests that implement cells. It stacks on #484. The tenancy check (#473) already calls POST /cells/v1/placements when a cell in enforced meets an org it does not hold; this is the service that answers it, with the directory the design describes ("The router"). Routing requests by host and issuer (rules 1 to 4), the routing cache and its snapshot come in the next pull request; the directory's operator commands (loom cell attach, place, limit, credential) after that.

Design

  • Where it lives. server/router/, a platform package (tests/unit/test_package_contracts.py), run by loom router serve from the same image as a deployment, on a release of its own. It takes the cell-name grammar, the fingerprint's version and defaults (server/core/cell.py) and the org-free names (server.auth.tenancy.ORG_FREE) from the cells' own modules. Its fingerprint rule (_agrees) is stricter than the cell's own gate (cell.matches): a field this build knows and the caller leaves out is judged at its default, since the caller, not the router, chose what to post; a field only the row's value names and the caller does not send is a newer build's, left to the cell's own gate, so an older pod behind an older router still places.
  • The directory (schemas/router_migrations/001_directory.sql, schema loom_router), in the router's own Postgres database:
    • cells: the row the design lists (host, state, mode, closed, quota, the SHA-256 of the cell's credential, deployment id, bucket, issuers, keys fingerprint, probe URL, break-glass bots, current and next fingerprints); at most one first cell;
    • placements: one row per org, never updated, deleted or truncated (triggers); the cell's placed counts them;
    • hosts, and audit (each placement, by cell:<name>, in the placement's own transaction).
    • It holds org names and cell metadata, never memory content: no erasure step runs there, and a placement outlives its org's erasure.
  • Durability. Every directory connection sets synchronous_commit = on and read committed, whatever the database's defaults, as the memory authority's pool does, and the pool's statements and idle transactions are bounded on the server (statement_timeout, idle_in_transaction_session_timeout), so a stalled router never keeps a cell's row lock: a placement the router answered survives a failover, and a later placement of one org sees the earlier one. A call's whole wait for a connection (a lookup's slot, the pool's opening, the connection) is bounded by the same STATEMENT_TIMEOUT_S, and lookups, which any client may send, hold at most half the pool's connections, so a lookup flood leaves placements their connections.
  • Migrations. pg_migrate.migrate takes the connection, ledger schema and advisory lock as arguments, defaulting to the memory authority's; the directory migrates with the same runner under its own lock and loom_router.pg_migrations.
  • POST /cells/v1/placements (server/router/app.py), the contract tenancy._place already speaks ({org, cell, fingerprint} in, {cell, deployment_id} out):
    • 400 for a malformed body (bounded at 64 KiB, or nested past the parser's depth), an org-free org or one Postgres text cannot hold (a NUL, a lone surrogate), or a cell name outside the grammar;
    • 401 for a request with no bearer, answered before the body is read, and when the bearer's SHA-256 is not the named cell's (hmac.compare_digest) or the directory has no such cell. The cells' credential hashes are kept in memory and read from the directory at most once per LOOM_ROUTER_DIRECTORY_CACHE_S (a failed read stands for the window too, answering 503), so a call with a wrong credential reads nothing and can lock no cell out; the placement checks the credential again on the locked row, and a credential the row no longer holds answers 401 there too, with the cached hashes read again on the next call;
    • 503, writing nothing, when the cell is past its rate this minute (with Retry-After: the seconds until its oldest counted call leaves the window, which the cell passes on to its client), not attached (neither serving nor the first cell attaching once the directory records enforced (attach step 3)), closed, past its quota, or calling with a fingerprint that agrees with neither current nor next: every field the row or this build names must be equal, a missing one at its default, each of the same type (a posted 1 is not a stored true), and the version must be the integer this build reads, so a field this build knows and the caller leaves out is judged, and a field this build does not know is passed over unless the caller sends it (a newer cell's or a newer row's, which the cell's own gate judges), or when the directory cannot be read;
    • otherwise the placement, in one transaction: a transaction-scoped advisory lock on the org, so two cells placing one org run one after the other and the later reads the earlier's winner; an org placed already answers its cell whatever the asking cell's state; else the asking cell's row is locked (FOR UPDATE) and judged as it is then (attached, open, fingerprint, placed under placements_max), the placement is inserted, placed counted, and the audit row written. A cell closed, limited or updated meanwhile is judged on its new row, and the quota holds across router replicas.
  • GET /cells/lookup?org=: the org's cell and host, or the first cell for an org with no placement; unauthenticated, limited per client address (429 with Retry-After, from the same window), 503 while the directory cannot be read or holds no cell.
  • Limits (server/router/limits.py): a 60-second window per key, counted by each replica (LOOM_ROUTER_PLACEMENTS_PER_MINUTE, LOOM_ROUTER_LOOKUPS_PER_MINUTE), at most 10,000 keys remembered. The directory's quota is the bound across replicas.
  • Health: /livez, and /readyz while the directory answers, read at most once a second however often it is probed.
  • Settings: LOOM_ROUTER_PG_DSN (a secret: the pattern now matches any *_pg_dsn), the two limits, and LOOM_ROUTER_DIRECTORY_CACHE_S. loom router serve and migrate refuse without the DSN, and with one equal to LOOM_PG_DSN; migrate also refuses a database that holds a memory authority's ledger (loom.pg_migrations): the directory is never a cell's database.

Scope and risk

Nothing runs unless loom router serve does. The server's only change is the migration runner's new arguments, whose defaults keep its behaviour (tests/unit/test_pg_authority.py passes).

The placement rate is per cell and counted by each router replica; only placements_max holds across replicas, as the page says.

The design also rotates a cell's router credential (loom cell credential --next, --finish): the directory then holds a next hash beside the current one and the router accepts both. That comes with the directory's operator commands; until then a cell has the one hash credential_sha256 records, and loom cell limit --closed, which writes only the directory, is the stop for a leaked one once that command lands.

Two router pieces the page describes come after this one, before loom cell prepare ships: the state read in #519 and the lookup cache and NO_CELL in #520. As first planned they were to come with the routing pull request (no command writes a mode row until then): the state read GET /cells/v1/cells/<cell>, which a first cell in recording makes before it records a new org (until it lands, ensure_held records whenever the mode row reads recording), and the cache of placed lookups that keeps answering them while the directory is down (until then every lookup answers 503 in an outage). Before any cell is registered a lookup answers 503, as the page now says.

The lookup's clients come with that pull request too: loom login --org, which starts a flow at the host the lookup names, keeps the host it was given on the no-cell answer (#520 gives that answer the code NO_CELL, which the client the routing pull request adds matches instead of the message) and on a 404 from a deployment with no router, waits a 429's Retry-After for at most --wait (default 2 minutes) and then refuses as a rate refusal, and refuses on any other answer; the first cell's sign-in page that asks for the org first; the typed WRONG_CELL error in the SDKs comes in #521.

Compatibility, rollout and rollback

  • Wire: two new routes on a new process; no change to a cell's API.
  • Rolling upgrade: no change to a cell's schemas. The directory's schema is new.
  • Operator steps: none until a router is deployed (loom router migrate, then loom router serve).
  • Rollback: revert.

Tests

  • tests/unit/test_router.py (54), against a fake directory: a placement and its retry; an org placed elsewhere answering its cell from a closed cell, an attaching cell, a fingerprint matching neither value, and a replaced credential still cached; a retry of an org placed already counted against the rate; a flood of wrong credentials reading the directory once and leaving the cell's own call placing; no bearer reading nothing; readiness reading the directory once for ten probes and again past its second; a replaced credential hash placing once the cache window passes; the replaced credential answering 401 within the window and placing nothing; an unreadable directory read once per window; fingerprints with fields left out refused, a field only a newer cell posts passed over, and a field only a newer build's row names left to the cell's gate when the pod does not send it; a credential naming another cell placing nothing there; 401 for a wrong or empty credential and an unknown cell; 400 for each malformed body, including one past the bound; 503 and nothing placed for each refusal (attaching, a first cell before enforced, closed, unmatched fingerprint, quota) and an unreadable directory; the first cell placing once attach set enforced; a next value placing; fingerprints of another shape; the per-cell rate; lookups (placed org, first cell, none yet, no org) and their per-client limit; the limiter's window, key bound and its Retry-After wait; a rate refusal carrying Retry-After; the directory connections: lookups holding at most LOOKUP_SLOTS (half the pool) while a placement still gets a connection, and a wait past STATEMENT_TIMEOUT_S answering the directory unavailable; and the refusal of a missing or shared DSN.
  • tests/integration/test_router_directory_pg.py, against a Postgres database of its own: the migrations apply and then find nothing pending; a pool connection shows synchronous_commit = on, read committed and the two server-side timeouts; two cells placing one org while its lock is held elsewhere both wait (seen in pg_locks), read back one winner, and leave one audit row, the winner's; ten concurrent placements in a cell with a quota of three write three, refuse seven and count three; an org placed already answers its cell whatever the cell's state; a refusal on the locked row writes nothing; UPDATE, DELETE and TRUNCATE on placements are refused; a second cell recording another's credential hash is refused (UNIQUE); migrate refuses the memory authority's database. It passed against a local Postgres.
  • CI: the Postgres integration job creates a second database for the directory (LOOM_ROUTER_PG_DSN) and runs this file (A cell serves only the orgs it holds, and places a token's new org through the router #473 already runs tests/integration/test_cell_tenancy_pg.py there).
  • test_package_contracts, test_cli_surface_snapshot, test_settings_defaults, test_doc_links, test_public_copy, test_pg_authority, and every unit test naming the CI workflows: pass. Lint and typecheck pass.
  • Full unit suite: not run on this stack yet; it runs before the stack merges.

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-router-directory branch from f231077 to ff70202 Compare November 10, 2026 17:53
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from ff70202 to 3e31c94 Compare November 10, 2026 18:03
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 3e31c94 to d3938ba Compare November 10, 2026 18:19
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from d3938ba to 7a95877 Compare November 10, 2026 18:29
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 7a95877 to 53b7498 Compare November 10, 2026 18:38
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 53b7498 to 8383574 Compare November 10, 2026 18:44
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 8383574 to daae05f Compare November 10, 2026 18:55
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from daae05f to f4ecbd8 Compare November 10, 2026 19:02
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from f4ecbd8 to 60d08cf Compare November 10, 2026 19:08
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 60d08cf to 981c2fa Compare November 10, 2026 19:16
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 981c2fa to 8cb4810 Compare November 10, 2026 19:26
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 8cb4810 to 929cc06 Compare November 10, 2026 19:39
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 929cc06 to 4e38927 Compare November 10, 2026 19:44
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 4e38927 to 0d530d8 Compare November 10, 2026 19:54
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 9843da4 to a9f693d Compare November 10, 2026 21:20
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from a9f693d to 04dd2b3 Compare November 10, 2026 21:31
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 04dd2b3 to cc282a1 Compare November 10, 2026 21:48
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from cc282a1 to 0da0733 Compare November 10, 2026 21:55
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 0da0733 to 6540f8d Compare November 10, 2026 22:00
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 6540f8d to efd90d1 Compare November 10, 2026 22:06
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from efd90d1 to bef0d53 Compare November 10, 2026 22:14
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from bef0d53 to 605c59f Compare November 10, 2026 22:23
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 605c59f to 185f57d Compare November 10, 2026 22:29
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 185f57d to f51f337 Compare November 10, 2026 22:35
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from f51f337 to bc38a97 Compare November 10, 2026 22:41
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from bc38a97 to 8e96148 Compare November 10, 2026 22:49
@zlareb1
zlareb1 force-pushed the feat/cell-router-directory branch from 8e96148 to e8086ce Compare November 10, 2026 22:54
…d a client finds an org's cell

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@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