Skip to content

A cell's process serves only while its own rows in Postgres accept its configuration - #468

Merged
zlareb1 merged 10 commits into
mainfrom
feat/cell-own-state
Nov 11, 2026
Merged

zlareb1 merged 10 commits into
mainfrom
feat/cell-own-state

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 process with LOOM_CELL set serves only while its cell's rows in Postgres are read and accept its configuration; until then it answers 503 AUTH_UNAVAILABLE to everything but its health probes and runs no worker. With LOOM_CELL unset nothing changes.

Why

The first of the pull requests that implement cells (the design is #452, which this stacks on and which merges with the stack). A cell must never serve with a configuration its directory row did not accept, for example a cell set up to accept another cell's issuer or a host it was not attached with. The check lives in each process, so it holds wherever a request reaches the process, not only behind the router's readiness gating. Later pull requests write these rows (loom cell prepare, attach, update) and add the tenancy check, the router, per-cell SCIM and the enforced-mode org triggers.

Design

  • Settings: with the SCIM refusal, before anything touches a store, LOOM_CELL (1 to 32 of a-z, 0-9, -; "" = not a cell) and LOOM_TENANCY_MODE_CACHE_S (default 5 s, at least 1, beside the keyring's refresh: LOOM_JWT_REVOCATION_MAX_STALE_SECONDS must be at least three times it, as for the keyring, a check that applies only with LOOM_CELL set). A process with LOOM_CELL set refuses to boot without the Postgres authority, authentication on every transport, or LOOM_PUBLIC_BASE_URL.

  • Postgres (008_cells.sql):

    • loom.cell_first: one row, which triggers refuse to update, delete or truncate;
    • loom.tenancy_mode: recording or enforced;
    • loom.tenancy: one row per org the cell holds, never deleted;
    • loom.cell_self: the accepted fingerprints, current and, during a change, next, each a JSON object by constraint.
      All four are classified as holding no memory content.
  • ClickHouse (094_cell_heartbeats.sql): loom.cell_heartbeats, one KeeperMap key per process beside its loom.instances row, written with every beat. It holds the LOOM_CELL the process runs with, the mode it last read and its fingerprint. Prepare and attach will compare these across a cell's processes. It is a table of its own because ClickHouse refuses ALTER TABLE … ADD COLUMN on a KeeperMap table (NOT_IMPLEMENTED). Every build from this one reports its fingerprint, even outside a cell, because prepare reads it before LOOM_CELL rolls out. Under the Postgres authority that fingerprint carries the deployment id from boot on, LOOM_CELL set or not, so the first cell's processes report the same value before and after the rollout.

  • The fingerprint (server/core/cell.py) is a set of named fields under a schema version:

    • the hosts of the public base URL and the resolved OAuth issuer (one normalise_host);
    • the OIDC issuer and client id, and whether SCIM is on;
    • the bearer issuers and the configured orgs (oidc_jit_org, an open signup's org, LOOM_CODE_ORG, the org of each LOOM_CODE_REPOS_JSON entry the code config keeps, and LOOM_TRACKER_ORG; a test checks the repository orgs against the code config itself, since server.core may not import it);
    • the end-user issuers' org patterns;
    • the deployment id this process verified at boot, and the bucket name and prefix (the prefix only with a bucket).

    A process matches a value when every field both know is equal; a field the value lacks counts as equal only at its default. An added field keeps the version; a changed meaning raises it, and a build matches only values of its own version. Two builds with one configuration therefore match one row.

  • The gate: with LOOM_CELL set, the gate holds while any of the three rows is unread, while one is absent, while the first row names another cell, or while the fingerprint matches neither current nor next. Then:

    • every request but the health probes answers 503 AUTH_UNAVAILABLE, from the middleware that already refuses a drifted process, so sign-in, minting, OAuth, device, SCIM and pages are covered;
    • verify_token checks the gate first, before the end-user issuer branch (CellUnavailable, an AuthUnavailable);
    • readiness fails (cell), and the heartbeat's serving is 0;
    • the process stands aside from the workers in every mode: in auto as a drifted process does, and in on the lease loop now runs for a cell and starts or stops the supervisor on the gate, starting nothing at boot. Only the deployment's leader registers tracker namespaces when it starts them, as at boot.

    The three rows are re-read in one statement every LOOM_TENANCY_MODE_CACHE_S; a failed read, or a row that does not decode as this build reads it, keeps the last rows for at most LOOM_JWT_REVOCATION_MAX_STALE_SECONDS, the bound the signing keyring trusts its own snapshot for, and is retried on schedule; past it the process is gated until a read lands. The workers child reads them itself, since its passes verify tokens. The verdict is computed once per read, not per request.

Alternatives weighed: a readiness check alone, which a pod IP, a port-forward or a deployment without readiness gating bypasses; and new columns on loom.instances, which KeeperMap refuses.

Scope and risk

Every code path is behind settings.cell, with two exceptions: the cell heartbeat, a single extra Keeper write per beat that every process makes, and, under the Postgres authority, one read of loom.authority at boot for the deployment id the fingerprint carries. Nothing writes the cell rows yet, so a process with LOOM_CELL set stays gated until loom cell prepare lands; the setting is not useful before that pull request.

The design also has a process with LOOM_CELL unset read, in one statement, the mode and first rows and whether any loom.tenancy or loom.cell_self row exists, and be gated (health probes and the probe route still answering) in every state but open (both rows absent with no tenancy or own row) and the first cell in recording. Nothing writes enforced until attach lands, so that gate comes with the pull request that reads the router's cell state, with its test; here a process with LOOM_CELL unset reads no cell row.

Compatibility, rollout and rollback

  • Wire: none. New error source: AUTH_UNAVAILABLE from a gated cell.
  • Migration numbers: 008_cells.sql follows main's 006_erasure_ledger.sql and 007_knowledge_datasets.sql, so a build N-1 reads it as a newer build's file; 094_cell_heartbeats.sql follows main's 093_service_account_saves_to.sql. schemas/schema.sql is dumped from a ClickHouse migrated from empty in that order.
  • Rolling upgrade: build N-1 writes no loom.cell_heartbeats row, and prepare (later) reads a live instance without one as a build before cells. Neither migration changes an existing table. The Postgres migration that adds the org triggers (cells.md, "Rolling upgrades") comes with attach in a later pull request of the same release; 008_cells.sql adds none. Its loom.cell_first triggers, which refuse an update, delete or truncate of the first row, are ENABLE ALWAYS, so they fire under session_replication_role = replica too (checked on Postgres 16).
  • Operator steps: none.
  • Rollback: revert. The new tables stay, unread.

Tests

  • tests/unit/test_cell.py test_only_the_first_cell_reads_recording: only the first cell ever records. On a later cell (first read as (false, …)), cell.mode() reads enforced whatever the mode row says, so the tenancy check and the CLI place, never record, there; the gate still reads the raw row, so an absent mode row still gates (test_a_cell_is_gated_until_its_rows_accept_it; the table's CHECK refuses any other value).
  • tests/unit/test_cell.py (105, parametrized cases included):
    • the configured orgs hold the org of each LOOM_CODE_REPOS_JSON entry the code config keeps, compared with repos._configured_from_env over kept, skipped and malformed entries, with LOOM_CODE_ORG and LOOM_CODE_REPO set and unset;
    • a cell's on process registers tracker namespaces only with LOOM_DEPLOYMENT set;
    • host normalisation;
    • field-wise matching, including older and newer builds' values;
    • every gate condition, and current or next accepted;
    • a failed read keeping the last rows, and the one-statement read;
    • the boot refusals;
    • verify_token refusing first;
    • the middleware's 503 on sign-in, OAuth, SCIM, pages and MCP, its websocket close (1013) before accepting, and its health probes still answering;
    • worker stand-aside in auto and on: start_lease_loop runs for a cell's on process, and keep_lease starts, stops and restarts its supervisor as the gate opens, closes and opens;
    • the heartbeat's serving and cell row;
    • an invalid host, a fingerprint of another version, an unprepared Postgres and a malformed own row read through refresh, and the stale bound;
    • the workers child reading the cell rows before its supervisor starts, and a process without the deployment id reading it before its rows;
    • a failed instance beat still writing the cell beat, and either table's failed sweep leaving the other's to run.
  • test_schema_baseline, test_settings_defaults (snapshot), test_every_table_is_classified and test_pg_authority pass with the new tables and settings.
  • End to end on a private ClickHouse and Postgres, with a server from source as cell eu-1:
    • before its rows existed, the server was unready (cell: this cell's first row is absent), answered 503 AUTH_UNAVAILABLE and started no worker;
    • after the three rows were written from its heartbeat's fingerprint, it was ready, the 38 workers started and an unauthenticated call got 401;
    • a current that no longer matched, with this process's value as next, still served;
    • with next cleared it answered 503 and stopped its workers within one refresh;
    • UPDATE and DELETE on loom.cell_first were refused by the trigger.
  • tests/integration/test_cell_heartbeat.py: a running server's loom.cell_heartbeats row sits beside its instance row with cell and mode empty, every fingerprint field present, and, under the Postgres authority, the deployment id both stores record. It passed against a local server on the Postgres authority at this head.
  • Order: test_cell.py, test_pg_authority.py, test_instances.py and the mutation inventory, run in one process before test_code_compiler_tier_runner.py, pass (a test of the workers child had left the process's lease set). The full unit suite has not run since the rebase onto main; it runs before merge.
  • Full unit suite at c95a998 (before the rebase): 32521 passed, 8 failed. test_deeply_nested_hidden_html_parses_in_bounded_time fails on this machine on main; the other seven (five in test_decision_retirement.py, two in test_agent_claim.py) pass when run alone at this head (7 passed): load on a shared host. Earlier runs found the untested LOOM_TENANCY_MODE_CACHE_S, the exception text in the cell's reason, a boot test missing the deployment-id stub and a stale settings schema, all fixed.
  • Against a real Postgres: a scalar current_fp is refused by the constraint, TRUNCATE loom.cell_first by the trigger, and rows read through asyncpg's JSON codec make the cell ready.

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 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 docs/pg-placement-units branch from 9397c1c to f4d106d Compare November 10, 2026 16:22
@zlareb1
zlareb1 force-pushed the feat/cell-own-state branch 28 times, most recently from 435bd7b to 4dd3a95 Compare November 10, 2026 21:58
zlareb1 and others added 10 commits November 11, 2026 21:04
…s configuration

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…nt: tested; one refusal helper; the migration header names the rows prepare writes

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ing comments

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…de config keeps, checked against the code config; the mode cache's comment states its bound

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…lformed value; the test covers an unset code org

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…n at boot, not by parsing its settings

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…LOOM_CELL

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
…ent names only its boot hand-ins

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