Skip to content

Run Loom from ClickHouse and its Keeper, with every instance a full replica - #32

Merged
zlareb1 merged 1 commit into
mainfrom
feat/ch-only-portability
Oct 16, 2026
Merged

zlareb1 merged 1 commit into
mainfrom
feat/ch-only-portability

Conversation

@zlareb1

@zlareb1 zlareb1 commented Oct 14, 2026 •

Copy link
Copy Markdown
Member

Related: #29
Related: #35

Changelog category (leave one):

  • Backward Incompatible Change

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 keeps all of its state in ClickHouse, so you can run any number of identical replicas behind one URL and move a deployment by moving its ClickHouse, its env and LOOM_JWT_SECRET. The ClickHouse server now needs a Keeper (the bundled one has one, and so does ClickHouse Cloud).

Why

Loom kept some state on the host it ran on: uploaded originals on a blob volume, rate-limit overrides in a file, the workers lock as a flock, graph-walk marks in a directory, sign-in counters in memory. A second replica, or the same ClickHouse restored somewhere else, lost all of it. This moves that state into ClickHouse and its Keeper.

Was Now
Originals in LOOM_BLOB_DIR loom.document_blobs, keyed by sha256. loom blobs import <dir> --apply moves an old directory in once.
LOOM_JWT_SECRETS_JSON / ACTIVE_KID A keyring in loom.signing_keys, sealed with LOOM_JWT_SECRET (still kid default), managed with loom signing-key. A new key verifies everywhere before it signs anywhere.
Override file loom.runtime_overrides, picked up by every process within LOOM_RUNTIME_OVERRIDES_REFRESH_SECONDS.
Workers flock A lease in Keeper. One leader runs the workers, in a child process.
Graph-mark files loom.graph_projections.
Sign-in counters in memory loom.auth_attempts, counted with sequential reads.
Read-replica mode Gone. Every process serves everything.

Git mirrors stay a cache that each replica clones on start.

Three things shaped the design.

Anything decided once (a sign-up name, a spent OAuth code, a grant change, the workers leader) has to be decided once for the whole deployment, so it is a claim in Keeper (a KeeperMap key under strict mode). That is the only compare-and-set ClickHouse has. I first had a fallback for servers without a Keeper (insert a row, wait, read back), but a review showed it can let two holders in while an earlier insert is still in flight, so it's gone: Loom refuses to start without a Keeper. The claims from #29 move onto the same leases.

Replicas lag each other, so reads that must see another replica's acknowledged write use sequential consistency (the keyring, sign-in counts, OAuth state, usage credits).

During a rolling upgrade the old and new builds write the same tables, and an old build's version row would reset a column only the new build knows. Each process acknowledges the column lists it writes with in loom.carry_acks, and a booting process waits for every live process to acknowledge its schema before it serves. ClickHouse also checks each carried insert itself: the batch goes out as INSERT … SELECT … FROM input(…) with a throwIf that refuses it if the table has gained a column, or if it starts after its deadline, so a request stuck in a proxy past an ALTER lands nothing. It adds about 5 ms per memory.set at p50.

Other changes that fell out of this:

  • /v1/health/live and /v1/health/ready for load balancers. On SIGTERM a replica stops being ready first, keeps serving LOOM_SHUTDOWN_READY_DELAY_S, drains, and does its last ClickHouse writes under one deadline that fits the 75 s stop grace.
  • Rate limits divide by the number of live replicas (from loom.instances) and never drop below one request.
  • An embedding or model change waits until every process has drained, including writes still running in ClickHouse. A process that died is retired by the next process on the same host once it can prove the old one is gone; otherwise loom participant retire does it.
  • Conflict verdicts record the write they judged, so a late verdict can't land on a newer revision.
  • Backups dump every MergeTree table, and restores stage and attach them under a claim on the target database.
  • The dashboard is served at /. Old /me links redirect.

Scope and risk

This touches every place that held per-host state: auth (keyring, sign-in limits, OAuth, sign-up), the version-write path, the workers leader, provider reconfiguration, backup and restore, install.sh and loom ch-setup, and the compose stack. Tenant isolation and erasure keep their shape, and the new tables are in the erasure census.

Known limits:

  • Leases and write deadlines assume host clocks agree to within a few seconds.
  • A refines verdict that lands after its source was revised acts as if it landed just before the revision, so the newer revision's conflict link can name a superseded row (only visible in the review queue).
  • Erasure waits for its mutation on one replica, so another replica can briefly still show the document.
  • On a self-managed ReplicatedMergeTree cluster, point every Loom process at one replica: sequential reads there only see quorum inserts. loom doctor warns about this.

Compatibility, rollout and rollback

  • Migrations 033_clickhouse_only_state to 038_embedding_write_sha, all additive.
  • The ClickHouse server needs a Keeper. The bundled stack has one. For your own server, put deploy/clickhouse-config/keeper.xml in its config.d/ (or add a <zookeeper> section for a Keeper you already run) and restart it. Boot, loom migrate, the preflight and loom ch-setup all say so if it's missing, and install.sh --bundled switches an install back to the bundled ClickHouse.
  • If you use LOOM_JWT_SECRETS_JSON, run loom signing-key import --active <kid> before the new build serves (add --algorithm RS256 for an RS256 map). Boot refuses while those variables are set.
  • Run loom blobs import <old LOOM_BLOB_DIR> --apply once. Until then older originals answer 410.
  • Overrides set at runtime need setting again.
  • Removed settings: LOOM_JWT_SECRETS_JSON, LOOM_JWT_ACTIVE_KID, LOOM_SUPERVISOR_LOCK_PATH, LOOM_BOOT_MIGRATE_LOCK_PATH, LOOM_BOOT_MIGRATE_LEASE_SETTLE_S, LOOM_GRAPH_WRITE_MARKS_*, LOOM_TASK_LEASE_BACKEND, and LOOM_READ_REPLICA_*. New ones are in docs/reference/configuration.md.
  • Behind a load balancer, LOOM_TRUSTED_PROXIES has to name it, or every client shares one sign-in limit.
  • While replicas of the previous build are still serving, an OAuth exchange or grant change on an old replica isn't kept apart from one on a new replica, since they claim in different places. The oauth_claims and grant_claims tables from Cap token claims at the live grant, serialize grant changes, and bound all_mine search #29 are no longer written and can be dropped in a later release.
  • Rollback: the migrations are additive, so the previous build runs on the new schema, but it doesn't read the keyring, the imported blobs or the runtime overrides. Restore its env and blob directory before rolling back past this.

Tests

  • Unit: 18,610 passing in random order.
  • Integration: 923 passing with three web workers on ClickHouse 26.9 against a fresh database.
  • make test-mixed-version: the previous and this build side by side on one ClickHouse.
  • scripts/gates/docker_quickstart_smoke.py against a freshly built image, including a SIGKILL in the middle of a provider rebuild.
  • Live ClickHouse tests for the carry fence: a held request refused after an ALTER, a cancelled insert held until it ended, a request refused after its deadline, and a boot that waits out a killed writer's running insert.
  • Fault-injection tests for shutdown: with ClickHouse stalled, the instance stop, retirement, withdrawal and close all finish inside the budget, and a stuck heartbeat thread doesn't hold up process exit.

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, checked by a separate reviewer agent and by external review over many rounds. Every finding was fixed with a regression test.

  • 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 plugin hook change 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 changed the title Make Loom portable on ClickHouse alone Make Loom stateless: portable on ClickHouse alone, every instance a full replica Oct 14, 2026
@zlareb1
zlareb1 force-pushed the feat/ch-only-portability branch 10 times, most recently from afef00d to 45f25aa Compare October 15, 2026 14:53
@zlareb1 zlareb1 changed the title Make Loom stateless: portable on ClickHouse alone, every instance a full replica Run Loom from ClickHouse alone, with every instance a full replica Oct 15, 2026
@zlareb1
zlareb1 force-pushed the feat/ch-only-portability branch 2 times, most recently from 3bb0553 to d5b5b1a Compare October 15, 2026 16:39
@zlareb1
zlareb1 force-pushed the feat/ch-only-portability branch 15 times, most recently from 4975c1b to 8eedfa8 Compare October 16, 2026 09:20
@zlareb1
zlareb1 force-pushed the feat/ch-only-portability branch 2 times, most recently from 427790c to 5583ac2 Compare October 16, 2026 11:12
…ull replica

A Loom process pointed at the same, a moved or a restored ClickHouse gets
every feature back: uploaded originals, signing keys, admin overrides,
sign-in limits, the workers lease, graph-walk marks, deferred conflict
judgements and similarity calibration live in ClickHouse and its Keeper
(migrations 028–033). N identical replicas serve behind one URL without
sticky sessions, builds N-1 and N side by side; readiness, drift checks and
a column-carry barrier keep a rolling upgrade from losing writes. The design
rationale, review rounds and upgrade notes are in the PR description.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: zlareb1 <[email protected]>
@zlareb1
zlareb1 force-pushed the feat/ch-only-portability branch from 5583ac2 to eb62fb6 Compare October 16, 2026 11:36
@zlareb1 zlareb1 changed the title Run Loom from ClickHouse alone, with every instance a full replica Run Loom from ClickHouse and its Keeper, with every instance a full replica Oct 16, 2026
@zlareb1
zlareb1 merged commit 5c54712 into main Oct 16, 2026
21 checks passed
zlareb1 added a commit that referenced this pull request Oct 16, 2026
…-038

Main's #32 took 033-038 for ClickHouse-only and shared replica state, so this
PR's graph-row, abandoned-failure, seen_at and restore_id migrations follow it;
none of main's DDL touches the tables or columns they add.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zlareb1 added a commit that referenced this pull request Oct 16, 2026
Main's #32 replaced the file-backed GraphWriteMarks object with module
functions, so the PR's ALTER-based closes and reopens called an API that no
longer exists and mypy failed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zlareb1 added a commit that referenced this pull request Oct 16, 2026
…irror flag

Main's #32 moved the key write lock onto Keeper leases, which the Undo now holds,
and added a judged argument to the status mirror the tests replace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zlareb1 added a commit that referenced this pull request Oct 16, 2026
… a cluster lease and its single-process mode goes

Main (#32) took migrations 033-038, so bi-temporal memory moves to 039. With a
Keeper required everywhere, LOOM_MEMORY_TEMPORAL_PUBLISHER, the in-process
publisher, the second-writer check, memory.temporal_status's publisher field
and the memory_writes_refused problem are gone: a namespace's commits are
serialized by leases.cluster_lease, renewed while held. Main's revisions to
memory.update (the deferred judge gets the vector's version, time and hash),
write_versions_by_ids (judged) and the lock wording are carried into this
branch's reworked paths.

Co-Authored-By: Claude Opus 5.5 <[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