Skip to content

Calibrate a code namespace after its first embed and after an embedder change - #2022

Merged
zlareb1 merged 7 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/code-calibrate-on-embed
Dec 3, 2026
Merged

zlareb1 merged 7 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/code-calibrate-on-embed

Conversation

@KaloyanNaumov

@KaloyanNaumov KaloyanNaumov commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: A new worker, off by default, calibrates a code namespace once it has finished embedding under the served embedder and has no calibration under it, which covers its first embed and any embedder change. A calibration row written under another embedding space (another model, or the same label at another endpoint or width) now reads as missing, so the cluster thresholds serve until the worker (or code.calibrate) replaces it. Together with #2011 (the probes code.calibrate samples from the namespace's own code), this closes #1558.

Closes #1558

Changelog category (leave one):

  • Improvement

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

With LOOM_CODE_CALIBRATION_ENABLED on, Loom calibrates each code namespace after its first completed embed and again after an embedder change; a namespace calibration written under another embedding space no longer gates searches.

Why

code.calibrate runs only when an operator calls it. A code namespace nobody calibrated serves the cluster thresholds, and one calibrated under an old embedder keeps gating with a row from another cosine scale: get_namespace_calibration read embedder_model but never compared it with the served model; the row now stores the embedding space's key, as the shared calibration does, so a rebuild that keeps the label at another endpoint or width is caught too. Option 1 of #1558 asks for exactly this: calibrate a code namespace automatically, on its first completed embed and again when its embedder changes. This is the smallest form of it, off by default, so the answer can be a one-line change later. #2011 makes the calibration the worker writes derive from the namespace's own code.

One predicate covers both triggers: the namespace has no calibration row under the served embedding space, and it has finished embedding under that embedder: its first index is built (not waiting for loom code backfill and not in a building stage, since a first index embeds slice by slice and has no stub between two slices), no stub is left to embed under the served label (read as code_embed_recovery reads it), and at least one live chunk carries a vector under that label, read in the table that label's rows live in. A first embed is "row missing, finished embedding". An embedder change is "row names another embedding space, vectors now exist under the new one with no stub left"; vectors that exist only under the old label do not count. The predicate keeps no state, so a restart or a second replica repeats nothing: once the row exists, it answers false. The row is a ReplacingMergeTree, so two replicas that race write the same key.

The worker code_calibration walks the configured repositories, skips switched-off orgs and namespaces being erased, and calibrates at most one namespace a pass. It runs under the namespace's own customer and worker identity, so embedding spend stays attributed to it. The handler's body after identity resolution moved into calibrate_namespace(org, ns), which both call. An attempt is recorded before it runs and cleared when it succeeds. A failed calibration is also recorded in the pass outcome, and the worker skips that namespace for three pass intervals (it remembers attempts in process; a restart forgets them). A pass the supervisor cancels at its deadline leaves the attempt recorded, so it cools down like a failure; either way a namespace that keeps failing or running long does not hold the queue for the others. The pass has a deadline of one hour, sized to one calibration, instead of the 900 s default. With a fake embedder the pass does nothing, since every probe would fail.

The settings sit next to recalibration_enabled: code_calibration_enabled (false) and code_calibration_interval_seconds (3600).

Scope and risk

Code namespaces and the namespace calibration reader. The stale-row rule sits in the memory plane's shared reader, so it applies to memory namespaces as well: a row naming another embedding space reads as missing there too; a row that holds a bare label (written by the previous build) is read as the served space when the label is the served one, so it keeps its thresholds; the cluster calibration serves. That is the existing fallback for a namespace with no row. The stored embedder_model value is now model@fingerprint. The cluster probe and the derivation are unchanged. The worker reads the namespace's calibration row, its first-index mark and stage, one stub probe and one existence read of an embedded chunk (CodeRepo.has_embedded_chunk, a LIMIT 1 read), and calibrates only when all say so; an idle deployment pays a check per namespace an hour.

This PR is independent of #2011: it calls code.calibrate's handler body as main has it, and either lands first. Both touch handle_code_calibrate in server/code/app/admin.py; the second to land resolves a conflict there and in the settings schema and the defaults snapshot, where both PRs add an entry at the same place (keep both entries and regenerate).

Compatibility, rollout and rollback

  • Wire: none.
  • Stored value: loom.namespace_calibration_result.embedder_model holds the model and its embedding-space fingerprint, as loom.embedder_calibration does. No column changes. A row holding a bare label equal to the served label keeps serving its thresholds, so N and N-1 agree through a rolling upgrade; a bare label of another model reads as missing. Limit: a bare-label row written before an endpoint or width change under the same label is not detected until the namespace is calibrated once (code.calibrate, or the worker when enabled), which writes model@fingerprint.
  • Operator steps: none by default. New settings LOOM_CODE_CALIBRATION_ENABLED (off) and LOOM_CODE_CALIBRATION_INTERVAL_SECONDS (3600).
  • Rollback: turn the worker off; rows it wrote stay in loom.namespace_calibration_result and serve like any code.calibrate row, while their embedding space matches.

Tests

tests/unit/test_code_calibration_worker.py covers the predicate (missing row calibrates, another embedder's row calibrates, a rebuild at another endpoint or another width calibrates, a bare-label row under the served label is served, a bare-label row under another label calibrates, a legacy row keeps its thresholds with the worker off, a namespace still embedding or empty waits, a namespace in a building stage waits, vectors only under another embedder wait, a current row under the served space is kept), the key a row is written under, the one-namespace bound, a failed calibration, a failing namespace and a namespace cancelled at the deadline, neither of which holds the queue, a failed namespace retried after the cooldown, a fake embedder skipped without a log line, the pass deadline, the worker off unless enabled, and the stale-row read for a memory and a code namespace. Ran ruff check, ruff format --check, mypy on the touched modules, the mypy and function-length gates, every codegen --check, the worker-registry snapshot and the touched tests.

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 checked every line of the diff.

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

Checklist

  • Local format, lint, type checks and focused tests pass. The full make test-unit and applicable integration results are reported by CI; a full local run is not required before pushing.
  • 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), the Codex plugin copies (python3 scripts/codegen/agent_plugin_sync.py) and the standalone skill bundles (python3 scripts/codegen/standalone_skills.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.
  • I signed the Contributor License Agreement (CLA) if CLA assistant requested a signature.

Note

Medium Risk
Changes similarity gate resolution for all namespace calibration rows (memory and code), and the worker runs embedder probes that affect retrieval thresholds when enabled.

Overview
Adds an optional code_calibration background worker (off by default) that probes one configured code namespace per pass when it has finished embedding under the served embedder but has no calibration row for that embedding space—covering first embed and embedder/endpoint/dimension changes.

Namespace calibration storage and reads now key rows with model@fingerprint (like shared embedder calibration). get_namespace_calibration treats rows for another space as absent so cluster thresholds apply until code.calibrate or the worker refreshes them; bare-label legacy rows still serve during rolling upgrades until rewritten.

calibrate_namespace(org, ns) is extracted from the admin handler for the worker; CodeRepo.has_embedded_chunk supports the readiness predicate. New settings LOOM_CODE_CALIBRATION_ENABLED and LOOM_CODE_CALIBRATION_INTERVAL_SECONDS (default 3600), with a 1h pass deadline and 3-pass cooldown on failures.

Reviewed by Cursor Bugbot for commit cc5e3e4. Bugbot is set up for automated code reviews on this repo. Configure here.

KaloyanNaumov and others added 2 commits December 2, 2026 11:19
…r change

`code.calibrate` runs only when an operator calls it, so a code namespace
nobody calibrated serves the cluster thresholds. One calibrated under an old
embedder kept gating with a row from another cosine scale, because
`get_namespace_calibration` read `embedder_model` but never compared it with
the served model.

A calibration row written under another embedder now reads as missing, and a
new worker, `code_calibration`, off by default, calibrates a code namespace
that has no row under the served embedder and is drained under it (no stub
left to embed, one embedded chunk at least). One predicate covers the first
embed and an embedder change, keeps no state, and is safe to repeat on a
restart or a second replica. A pass calibrates at most one namespace; a
namespace that fails or runs past the pass deadline waits out a cooldown so it
does not hold the queue. The handler's body after identity resolution moves
into `calibrate_namespace(org, ns)`, which the handler and the worker share.

The settings `code_calibration_enabled` (false) and
`code_calibration_interval_seconds` (3600) sit next to
`recalibration_enabled`.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
…ed model

The calibration worker called a namespace drained when no stub was left under
the served model and any chunk, under any model, carried a vector. After an
embedder change the old model's vectors satisfied that while nothing was
embedded under the new one yet, and a first index built in slices has vectors
and no stub between two slices. Either way the namespace was calibrated on a
partial or mislabelled corpus, and the worker never looked at it again.

The worker now skips a namespace that still awaits its first index or is in a
building stage, and asks whether one live chunk carries a vector under the
served model, read in the table that model's rows live in
(CodeRepo.has_embedded_chunk, a LIMIT 1 read). Two tests cover vectors only
under another model and a namespace in a building stage. The module docstring
now describes the predicate without narrating the change.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
loom-docs Ready Ready Preview Dec 3, 2026 7:34am UTC
loom-telemetry Ready Ready Preview Dec 3, 2026 7:34am UTC

Request Review

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The worker skips recalibration after a supported embedding-space change that retains the model label.

A namespace calibration row carried only the model label, so a row written
before a rebuild at another endpoint, provider or width, under the same label,
still read as current. get_namespace_calibration accepted it and
needs_calibration skipped the namespace, which kept serving thresholds from
the old vector space.

The row's embedder_model now holds shared_key(model), the model and its
embedding space's fingerprint, the key the shared calibration is stored under,
and the read compares it with the key of the served model. A row whose key
differs, including one that holds a bare label, reads as missing, so the
cluster calibration serves and the worker recalibrates the namespace once.
No column changes, and a replica running the previous build serves the row as
before. Tests cover a rebuild that retains the label at another endpoint and
at another width, a row that holds a bare label, and the written key.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Comment thread server/settings/catalog.py Resolved
The cooldown test ran at the default interval, so no test named the
setting and the test inventory flagged it. Set an interval of 7 s and
assert the cooldown is three passes of it: a failed namespace is still
skipped 20 s later and tried again after 22 s.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same-label embedding-space changes and the interval setting now have regression tests. The new storage format still changes the thresholds served on a default upgrade.

The previous build stored the bare model label in a namespace calibration
row. Comparing every row by `shared_key` read those rows as missing, so a
deployment that kept the same embedding space lost its thresholds on upgrade,
and with the worker off nothing wrote them back. A replica of the previous
build kept serving them, so replicas disagreed during the rollout.

A bare label that equals the served label is now accepted as the served
space. A fingerprinted value is compared by `shared_key` as before, and a
bare label of another model still reads as missing. New writes store the
fingerprinted key. The limit: a bare-label row written before an endpoint or
width change under the same label is not detected until the namespace is
calibrated once, by `code.calibrate` or by the worker when it is enabled.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 5cf6df8. Configure here.

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Legacy rows now stay available on upgrade. needs_calibration treats the same bare-label row as current, so enabling the worker leaves it unfingerprinted. A later endpoint or width change under the same label still serves the old thresholds. Let the worker refresh a bare-label row while search can use it during rollout. Calibration thread

…-embed-r35

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
A namespace calibration row written before the fingerprint holds the bare
embedder label, which the reader accepts: search keeps serving it during a
rollout. `needs_calibration` read the row the same way, so no pass selected
that namespace, and an endpoint or width change under the same label kept
serving thresholds measured in the old space.

The worker now asks whether the row names the served space's key, through
`get_namespace_calibration_stored_key`. A bare label is probed and the row
rewritten under `shared_key`. `get_namespace_calibration` and
`_row_names_served_space` are unchanged, so replicas of the previous build
and this one still serve the same thresholds.

`test_a_bare_label_row_is_served_and_refreshed` pins the refresh,
`test_a_bare_label_row_the_worker_refreshed_reads_as_missing_after_a_width_change`
pins the recorded space, and
`test_a_legacy_row_keeps_its_thresholds_while_the_worker_is_off` pins the
read that must not change.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>

@zlareb1 zlareb1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Search still reads legacy calibration rows during rollout. The worker refreshes a bare-label row once the namespace is ready and stores the embedding-space key, so a later change under the same label calls for recalibration. The worker tests exercise the legacy row and the matching fingerprint.

@zlareb1
zlareb1 merged commit 639d2c5 into ClickHouse:main Dec 3, 2026
81 checks passed
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.

A code namespace that was never calibrated abstains on most correct hits, because it inherits the memory calibration

2 participants