Repository navigation
Calibrate a code namespace after its first embed and after an embedder change - #2022
Conversation
…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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
zlareb1
left a comment
There was a problem hiding this comment.
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>
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
left a comment
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 5cf6df8. Configure here.
zlareb1
left a comment
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.

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 probescode.calibratesamples from the namespace's own code), this closes #1558.Closes #1558
Changelog category (leave one):
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_ENABLEDon, 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.calibrateruns 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_calibrationreadembedder_modelbut 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 backfilland 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 ascode_embed_recoveryreads 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_calibrationwalks 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 intocalibrate_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) andcode_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_modelvalue is nowmodel@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, aLIMIT 1read), 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 asmainhas it, and either lands first. Both touchhandle_code_calibrateinserver/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
loom.namespace_calibration_result.embedder_modelholds the model and its embedding-space fingerprint, asloom.embedder_calibrationdoes. 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 writesmodel@fingerprint.LOOM_CODE_CALIBRATION_ENABLED(off) andLOOM_CODE_CALIBRATION_INTERVAL_SECONDS(3600).loom.namespace_calibration_resultand serve like anycode.calibraterow, while their embedding space matches.Tests
tests/unit/test_code_calibration_worker.pycovers 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. Ranruff 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
make test-unitand applicable integration results are reported by CI; a full local run is not required before pushing.docs/reference/configuration.mdand thedocs/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).main; no merged migration is edited.hooks/,lib/,bin/,skills/,.mcp.json, the Codexplugin-defaults.json) bumps both plugin manifests' version, and no other open PR uses that version.npm --prefix frontend run build) and its tests pass, iffrontend/changed.docs/describe the system as it now is, and this description names any operator steps and wire changes.tests/unit/test_public_copy.py).git commit -s), as CONTRIBUTING.md asks.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_calibrationbackground 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_calibrationtreats rows for another space as absent so cluster thresholds apply untilcode.calibrateor 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_chunksupports the readiness predicate. New settingsLOOM_CODE_CALIBRATION_ENABLEDandLOOM_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.