Skip to content

A cell serves only the orgs it holds, and places a token's new org through the router - #473

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

zlareb1 merged 1 commit into
mainfrom
feat/cell-tenancy

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):

A cell serves only the orgs it holds: a call for another cell's org answers 409 WRONG_CELL and writes nothing, and a token's own org new to the cell is placed through the router. With LOOM_CELL unset nothing changes.

Why

The second of the pull requests that implement cells (the design is #452; this stacks on #468, which gates a cell's process on its own rows). It adds the tenancy check: how a cell knows it holds the org a verified call acts in, records it before the first attach finishes, places a new one through the router after, and refuses another cell's.

Design

  • server/auth/tenancy.py (ensure_held), with LOOM_CELL set, for the org a verified call acts in:
    • a principal that may not act in the org (may_act_in_org: a token of another org) is refused first with the gate's 403, so it records, places and learns nothing, not whether the cell holds the org nor where it is placed;
    • an org the cell holds (loom.tenancy, or one this process has seen held: a held org is never wrong to serve) passes with no further read;
    • in recording, one statement inserts the org's row only while the mode is still recording, and a conflict with another process's insert reads again;
    • in enforced, a principal scoped to the org (its token's own org) asks the router to place it (POST /cells/v1/placements at LOOM_CELL_ROUTER_URL with LOOM_CELL_ROUTER_TOKEN, carrying the process's fingerprint). The router inserts the placement if absent and answers the winner (cell and deployment id). Placed here, the cell writes its row and serves; placed elsewhere, 409 WRONG_CELL and nothing written;
    • a platform admin (of any org, its own included) or a *-org principal, or a call with no principal, never places an org;
    • an org the router answered is placed in another cell is remembered with its cell, so a client calling the wrong cell costs one router call (a placement never changes; the map is bounded); this cell's name under another deployment id than the process verified answers 503;
    • an unreadable tenancy, no router configured, an unanswered call or a malformed answer answers 503, and nothing is written; a router refusal's Retry-After (1 to 60 s) becomes the 503's, so a client past its cell's rate waits as the router asks;
    • requests of one process arriving together for a new org share one router call (one task per org, shielded so a cancelled request leaves it running for the others, its outcome retrieved even when every waiter has gone; tests cancel one waiter and all of them), and LOOM_CELL_ROUTER_TIMEOUT_S bounds the whole call (asyncio.timeout), not each read;
    • is_held, which personal-namespace provisioning reads for a caller acting in another org, remembers an org it read as not held for LOOM_TENANCY_MODE_CACHE_S, so such a caller reads Postgres once a window;
    • '', '-' and '*' name no tenant and are not checked.
  • Where it runs, before anything is written:
    • the op gate (gates.authorize) checks the call's resolved org first, before check_acted_for and personal-namespace provisioning, for HTTP and MCP alike; over MCP, WRONG_CELL is a tool error (isError) at the op gate and at the pre-dispatch namespace screen alike, so a batch keeps its other answers; a call in the token's own org, as gates.resolve_org resolves it (a code.* or tracker.* call naming no org acts in its plane's org), is checked before the caller's defaults are read (they live in that org), a 503 there ledgered before it is raised (its rows under the token's org, so the scrub blanks them), so a call that names no namespace answers WRONG_CELL, not a validation failure, and the listings (initialize, tools/list) treat such an org as having no default; and the gate provisions the token's own personal namespace only where that org is the call's or its tenancy row is present (tenancy.is_held, read on a cache miss and placing nothing, so every replica does the same);
    • check_authorized, which the routes outside the registry authorize through;
    • admit_org (refuse_foreign_org, then the check), which every admin route whose body names its org now calls;
    • the routes that take an org another way: task watch, agent claim (no principal), self-signup and agent signup (no principal, after their per-address limits), ratelimit.set's org tier, and the admin account routes that write for a body's org;
    • TenancyMiddleware (server/transport/tenancy_gate.py), inside the maintenance middleware, checks the org the routes that act in the token's org act in (acts_in_token_org: /v1/me/…, SCIM Groups, the assistant; for an org-wide or org-less token on /v1/me/set-default-namespace and /v1/me/sharing-summary, the self_signup_org those routes fall back to, checked with no principal, so it is recorded or refused, never placed), exactly the routes the census marks token, so a platform admin whose own org lives elsewhere still reaches the routes that act in no org or in a held one; a request with no bearer, a malformed Authorization header, or a token that does not verify passes to the route, which answers it; a token the process cannot judge yet (AuthUnavailable) answers 503 there, never passing unchecked. Its refusals take the route's own shape: an RFC 7644 error on the SCIM routes, Loom's envelope elsewhere.
  • Telemetry for an org the cell does not hold. A request refused for another cell's org, or refused before its org was checked (malformed JSON, an unknown argument), still writes its loom.agent_events row (the policy witness holds the same scrubbed row), but with the org, identity tuple, subject, consumer, key, run and arguments blanked (tenancy.attributable): that org's rows belong to its own cell, whose erasure never reaches this one. A code.* validation failure writes no code trace for such an org. Worker rows keep theirs: a pass runs on the cell's own stores.
  • The route census (tests/unit/test_cell_tenancy.py) lists every route of the app outside the op registry as token (the middleware), named (its handler, or a function of its module it calls, calls a check, read from the syntax tree) or org-free with the reason, and fails on a route not listed, an entry naming no route, or a named route whose module calls no check.
  • WRONG_CELL (409) joins the error reference. The SDKs map it by status until The SDKs raise a typed WrongCell that names the org and its cell #521 adds a typed WrongCell (The router's directory and placement API: a cell places a new org, and a client finds an org's cell #497 adds /cells/lookup; the routing pull request adds the lookup's clients).
  • Org-like op fields: a census test lists every op input field other than org that names an org (from_org on memory.copy_to, memory.cross_promote, procedure.fork, each checked through check_authorized).
  • Settings: LOOM_CELL_ROUTER_URL, LOOM_CELL_ROUTER_TOKEN (a secret: its name now matches the settings' secret pattern; refused at boot unless it is lcr_ and 128 lowercase hex characters, since the directory keeps only its unsalted SHA-256, and a length no SHA-256 has, so the secret is never taken for its hash) and LOOM_CELL_ROUTER_TIMEOUT_S (default 5 s).

Left for later pull requests, as the design orders them: the recording cell's read of its own state from the directory (GET /cells/v1/cells/<cell>, before it records a new org; here a recording mode row records, and no shipped command writes one until prepare, which lands after that read), the CLI's org declarations, the enforced-mode Postgres trigger and the ClickHouse writer check (with attach), and the router itself and its directory.

Scope and risk

Configured orgs (oidc_jit_org, the self_signup_org of a cell with signup on, the code and tracker orgs) are placed at attach. A call with no principal (JIT, self-signup) for one the cell does not hold yet answers 503, a retry naming what places it (attach, or loom cell update once the cell serves), never WRONG_CELL; JIT runs the check before it writes an account (jit._provision_jit).

Not in this PR: workers writing only for orgs the cell holds (the design's rule for a later cell's code index before attach), which lands with the commands that prepare and attach a later cell. And the design has a recording cell record a new org only while the directory answers that the cell is attaching (docs/architecture/cells.md, "What each cell runs"). That needs a cell-state answer from the router, which the router pull request after #497 adds together with this check; until then a recording cell records as it does here. Merging first is safe because no command in this stack writes loom.tenancy_mode before that pull request: nothing here runs loom cell prepare, attach or loom cell restore, so no cell is in recording until the check exists, and those commands land after it.

Every path is behind settings.cell. The middleware returns at once with LOOM_CELL unset; with it set, a raw route's bearer is verified twice (the middleware, then the route), which these low-volume routes afford.

Compatibility, rollout and rollback

  • Wire: a new error code, WRONG_CELL (409), from a cell only.
  • Rolling upgrade: no schema change. A build without the check beside one with it only matters on a cell, which needs this release on every process before attach (the design's rolling-upgrade rule).
  • Operator steps: none.
  • Rollback: revert.

Tests

  • tests/unit/test_cell_tenancy.py (79 cases):
    • a router refusal's Retry-After becomes the 503's, clamped to 1 to 60 s, and 1 s when absent or unreadable;
    • a cache still reading recording after Postgres moved to enforced: the recording insert writes nothing and a scoped principal is placed through the router, a platform principal refused with no call;
    • a router credential other than lcr_ and 128 lowercase hex characters refused at boot, and an unset one accepted;
    • a configured org a call with no principal acts in answers 503 until attach places it, an unconfigured one WRONG_CELL, and JIT writes no account in an org the cell does not hold;
    • nothing is checked with LOOM_CELL unset, nor an org-free call;
    • a held org passes once and is remembered;
    • recording records, and an org another process just recorded is held;
    • enforced places a scoped principal's org here, with the call's URL, credential and fingerprint checked;
    • an org placed in another cell, or under another deployment id, answers WRONG_CELL and writes nothing;
    • a platform, foreign-org or anonymous principal never places;
    • a refused, failed, malformed or slow placement, no router, and an unreadable tenancy each answer 503;
    • the op gate checks before check_acted_for;
    • check_authorized checks;
    • admit_org refuses a foreign org before checking;
    • the middleware refuses a raw route's token org and leaves op routes, MCP, and requests without a verifiable bearer alone;
    • a request event for an org the cell does not hold names no tenant, while a held org's, a worker's and a non-cell deployment's keep theirs;
    • an MCP call for another cell's org answers a JSON-RPC tool error, and a batch through the /mcp endpoint (the namespace screen included) keeps both answers when one entry names another cell's org (this test fails without the screen's catch); a loom_memory_set with no namespace from another cell's org answers WRONG_CELL (fails without the check before the defaults); the pre-check resolves a code.* call's org as the gate does, and ledgers a 503;
    • on a route the middleware checks (/v1/me/sharing-summary), a missing, malformed or unverifiable bearer passes to the route with nothing checked;
    • a platform admin never places even its own org; a winner elsewhere is remembered (one router call for three requests); this cell's name under another deployment id answers 503;
    • a code.* validation failure leaves no trace for an unheld org;
    • the gate provisions no personal namespace in an unheld token org;
    • a platform admin of an org held elsewhere reaches /v1/admin/workers.status, /v1/admin/token.mint and /v1/whoami through the middleware;
    • personal-namespace provisioning in the call's org and in a held token org, and none in an unheld one; is_held reads the row on a miss and places nothing;
    • a token of another org is refused with 403 in recording and enforced, for an org held, unheld or remembered elsewhere, recording, placing and naming nothing;
    • the middleware answers 503 while it cannot judge a token, and its 503 carries the error's own fields;
    • the service-account routes act in the token's own org;
    • an org-wide or org-less token on the two sign-up-org routes is checked in that org with no principal; the MCP pre-check's refusal row names no tenant;
    • on a SCIM route the middleware's refusal is an RFC 7644 error; WRONG_CELL says to call a cell only when it names one;
    • the census of every route, checking that the middleware's routes are exactly the token ones, each named route's handler (or a function of its module it calls), and the census of org-like op fields.
  • tests/unit/test_settings_env_contract.py: the environment sweep gives LOOM_CELL_ROUTER_TOKEN a value of its form, so the variable is still checked to reach its field.
  • tests/integration/test_cell_tenancy_pg.py runs the module's own statements against Postgres: in a transaction it rolls back, a recording insert records once and conflicts after, enforced records nothing, and a retried placement insert writes one row; and, over separate connections, an insert held open makes attach's LOCK TABLE loom.tenancy_mode IN EXCLUSIVE MODE wait (the insert reads the mode row FOR SHARE), and an insert that starts while attach waits queues behind it, then reads enforced and records nothing; each wait is seen in pg_stat_activity, and the test ends its racing backends before it restores the mode and rows it touched. Without FOR SHARE the second test fails. Both passed against a private Postgres 16. CI's Postgres integration job runs the file (.github/workflows/ci.yml). A third test checks that loom.cell_first (A cell's process serves only while its own rows in Postgres accept its configuration #468) refuses an update, a delete and a truncate, also under session_replication_role = replica: it passed against a database built from A cell's process serves only while its own rows in Postgres accept its configuration #468's 008_cells.sql and fails against one without its ENABLE ALWAYS lines. It skips without the table or without a superuser; CI's loom role is the image's bootstrap superuser (dev/ci_start_postgres.sh), so it runs there. A later cell never records: A cell's process serves only while its own rows in Postgres accept its configuration #468's mode cache reads any mode but enforced as enforced there, so ensure_held places, never records, on a later cell.
  • test_sdk_errors_from_response has the new code's row.
  • test_package_contracts and test_transport_layering pass with the module in server/auth/ (it reads authz and the roles, which server/core may not).
  • End to end, a server from source as cell eu-1 in enforced (a private ClickHouse and Postgres) beside a stub router that places acme here and answers beta with us-1:
    • memory.set with an acme token: one placement call (URL, bearer and fingerprint as sent), the tenancy row written, the memory written, 200; a later memory.get made no router call;
    • memory.set with a beta token: 409 WRONG_CELL, no tenancy row, no memory;
    • a raw route (/v1/me/sharing-summary) answered 409 for beta and 200 for acme through the middleware; /v1/whoami without a bearer reached the route;
    • beta's refused calls, the WRONG_CELL and an earlier unknown-argument refusal, left no row naming beta in loom.agent_events (nor the rollups it feeds); before the telemetry fix in this branch they left three.
  • Full unit suite: two runs were stopped, the first by later commits on the files it read and the second by the host's memory pressure (a shared machine). Targeted at this head: tests/unit/test_cell_tenancy.py (79 passed), and with test_cell.py, the SDK-error and errors-reference tests and the doc links (457 passed); earlier, every tests/unit/test_mcp_*.py with the doc gates (623 passed), and earlier the tenancy, MCP, agent-claim, SDK-error and doc gates. The full suite is run before merge.

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); the integration suite passes if server behaviour changed.
  • 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-tenancy branch 3 times, most recently from 397f333 to ca8bcf8 Compare November 10, 2026 16:15
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 68a8a90 to f027958 Compare November 10, 2026 16:29
@zlareb1
zlareb1 force-pushed the feat/cell-tenancy branch 5 times, most recently from 038d76c to ce1c191 Compare November 10, 2026 17:31
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 4fbf5ce to cf01548 Compare November 10, 2026 17:43
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from cf01548 to 14756d1 Compare November 10, 2026 18:03
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 14756d1 to d8d68d3 Compare November 10, 2026 18:19
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from d8d68d3 to 5a4f90e Compare November 10, 2026 18:29
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 5a4f90e to 91a07de Compare November 10, 2026 18:37
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 91a07de to 2116808 Compare November 10, 2026 18:44
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 6c805a6 to 6fa170b Compare November 10, 2026 18:55
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 6fa170b to f2cff64 Compare November 10, 2026 19:01
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from f2cff64 to 09487d4 Compare November 10, 2026 19:08
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 09487d4 to 5a9a54e Compare November 10, 2026 19:13
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from b3fa649 to e1dbf1e Compare November 10, 2026 20:25
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from e1dbf1e to 6d00bd7 Compare November 10, 2026 20:31
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 6d00bd7 to 88c4880 Compare November 10, 2026 20:45
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 88c4880 to cb89ef2 Compare November 10, 2026 20:56
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from cb89ef2 to 610acf9 Compare November 10, 2026 21:14
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 610acf9 to 7479bc9 Compare November 10, 2026 21:30
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 7479bc9 to 3201ba7 Compare November 10, 2026 21:34
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 3201ba7 to 435bd7b Compare November 10, 2026 21:52
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 435bd7b to 4dd3a95 Compare November 10, 2026 21:58
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 4dd3a95 to 557964c Compare November 10, 2026 22:04
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 557964c to ee77c95 Compare November 10, 2026 22:11
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from ee77c95 to 853547c Compare November 10, 2026 22:18
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch from 853547c to a859730 Compare November 10, 2026 22:26
…rough the router

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