Skip to content

The recovery worker republishes counts and coverage when it drains a namespace - #1563

Open
KaloyanNaumov wants to merge 7 commits into
ClickHouse:mainfrom
KaloyanNaumov:coverage-refresh
Open

KaloyanNaumov wants to merge 7 commits into
ClickHouse:mainfrom
KaloyanNaumov:coverage-refresh

Conversation

@KaloyanNaumov

@KaloyanNaumov KaloyanNaumov commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #1556
Related: #1538, #1550

TL;DR: A namespace whose stubs code_embed_recovery embedded kept reporting its pre-embed coverage and searchable: false until a commit landed, so search skipped its vectors. The worker now republishes the counts and coverage rows of every namespace it wrote vectors to on its first pass that finds nothing left to embed, retries on the next idle pass if a republish fails, and on each full-scan pass, idle or not, notes any namespace whose published counts or coverage trail its chunks for the next idle pass, so a restart before the drain loses nothing. With #1550, merged, which does the same for loom code backfill, both writers #1556 names publish what they write.

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in a release)

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 namespace embedded by code_embed_recovery reports its embedding coverage, and is searched with vectors, once recovery has no chunks left to embed, even when its branch has no new commit.

Why

ingest_pass._post_steps republishes the shared table_counts and coverage rows only inside if plan.commits:. Two writers store vectors outside that path: the CLI embed phase and code_embed_recovery. Neither moves a watermark, and a reader serves the shared row whatever its fingerprint. A namespace whose branch head did not move therefore kept its pre-embed chunks_embedded and embedding_coverage, read searchable: false, and code.search skipped its vector leg. On our deployment a fork of vllm-xpu-kernels with 6,868 of 6,868 chunks embedded reported 0 after a clean pass that indexed no commit.

PR #1550, now on main, makes each CLI backfill phase republish at its end. This PR does the same for the recovery worker:

  1. _embed_rows notes each (org, namespace) whose chunk vectors it inserted.
  2. On an idle pass (the key scan found nothing above the watermark in either table), _republish calls refresh_coverage and then refresh_table_counts for each noted namespace that the pass scanned, the same two refreshes the ingest pass runs after a commit.
  3. Both refreshes take must_land=True, so a failed state write raises instead of being dropped. A namespace whose republish raised stays noted, and the next idle pass retries both rows. The failure is a warning line and an entry in the pass's errors; the pass does not fail.
  4. A noted namespace the pass did not scan (removed with code.repo.remove, or its org switched off) waits. A removed one is never written after its purge, and a switched-off one is republished once its org is back on.
  5. The noted set lives in process memory, so every full-scan pass (the first after boot, then every code_embed_recovery_full_scan_seconds), idle or not, calls published_chunks_behind from The CLI embed phase republishes only when behind, once, with coverage first #1581 for each configured namespace and notes it when either published row trails the chunks. That read compares the counts row and the coverage row's embedded figure with one chunk aggregate, so current counts over a stale coverage row (the previous build's vectors landing between its two writes) are caught too. It runs on non-idle full scans because the first one after a restart reads superseded stub versions from zero and is seldom idle. The set is the fast path; this check is what guarantees "retry until both rows reflect the completed embeddings" across a restart. It needs no new state row: the published rows already carry the figures, and nothing cheaper records that vectors landed (the counts fingerprint covers watermarks only). The comparison and the republish read under sequential_reads(), as the CLI's republish does, so a lagging replica neither makes a current row read as behind nor lets the worker overwrite a current row with older figures. A namespace with only a coverage row published (its counts write failed) is compared and republished too. A failed read is a warning and a pass error, like a failed republish, and the next full scan reads that namespace again.

"Drained" is recovery's own predicate, so poisoned rows, rows under another model label and reference fixtures never hold it open.

Changed from the earlier approach

The earlier head of this PR had every ingest pass with no commit check the chunk figures itself: a chunk_counts state row laid over table_counts, a fingerprint gate, a short-circuit once the published pending count reached 0, and a fourth copy of the pending-stub predicate. Review showed two problems with one root. A published pending count of 0 is only a snapshot, but the short-circuit treated it as permanent, so stubs added later at the same fingerprint were never recounted and the bootstrap ready check could flip on stale figures. A same-fingerprint republish from #1550 landing inside a pass also broke "converges next pass". The bug is two writers that do not publish what they write, so this head makes the remaining writer publish, in about 30 lines of the worker, with no new state row and nothing added to the ingest pass.

Dependency

Depends on #1581 and is stacked on it: it uses must_land on refresh_coverage and published_chunks_behind from there. Review this PR's own two commits on top; it merges after #1581.

Scope and risk

server/code/workers/embed_recovery.py (_republish and the noted set), server/code/store/index/status.py (must_land on refresh_table_counts, default off, so every existing caller is unchanged), docs/features/code-index/freshness.md. The CLI embed phase reuses _embed_rows, so it notes namespaces in its own process too; it never runs an idle pass, and it republishes at its end itself.

Cost: one full republish (the coverage aggregates and the seven count scans) per namespace per drain, nothing on a pass that embeds and nothing on an idle pass with no namespace noted, plus two point reads and one chunk aggregate per configured namespace per LOOM_CODE_EMBED_RECOVERY_FULL_SCAN_SECONDS (30 min by default). A coverage computation that keeps failing is retried once per idle pass (code_embed_recovery_idle_interval_seconds, 120 s) with an error line, until it succeeds or a commit pass republishes.

Not covered:

  1. Progress during a long recovery backlog. The figures move at the drain, not per batch; while any configured namespace still has stubs, no pass is idle. The embedding stage of a self-service entry still reads live coverage on every pass.
  2. Two copies overlapping (a lease handover, two colours during a deploy): each republishes what it embedded. A republish that read the table before the other copy's last insert and wrote after that copy's republish leaves the rows one batch behind until the next full scan, drain or commit.
  3. Gaps main already has, unchanged here: chunks a walk heal or re-chunk writes with inline vectors and no commit, and a bootstrap's first-poll snapshot. code_embed_recovery_enabled defaults to false, so a deployment that leaves recovery off relies on CLI backfill phases republish the namespace's counts and coverage #1550 and on commit passes.

Compatibility, rollout and rollback

  • Wire: none. No new state key, field or setting.
  • Rollout: a process on the previous build does not republish at drain, as today. A namespace in this state today is noted by the first full scan after the new build starts and republished on the next idle pass.
  • Rollback: the rows this writes have the shape every build reads.

How this was found

Found and reproduced on our deployment with AI assistance. Fixed on a fork, PR submitted.

Tests

  • tests/unit/test_code_embed_recovery_republishes.py (new, 9 tests, 11 cases). The fake holds the state rows by key and the chunk table as rows; the real refresh_coverage and refresh_table_counts run against it, the worker's run_pass embeds through it, and each test reads the rows back:
    • test_a_drained_namespace_publishes_the_counts_and_coverage_of_its_table: 2 of 5 chunks embedded and published. After one pass that embeds 3 and one idle pass, table_counts reads 5 of 5 and the coverage row 5 embedded, as the table does. A further idle pass writes nothing.
    • test_a_failed_republish_is_retried_on_the_next_idle_pass, parametrised over a failed coverage write, a failed counts write and a failed counts aggregate: the idle pass records one error line and the counts stay at 0; the next idle pass republishes both rows at 3 of 3.
    • test_a_namespace_removed_before_the_drain_is_not_republished.
    • test_a_restart_before_the_drain_still_republishes_on_the_full_scan_pass: the last vectors land, the noted set, watermark and full-scan clock are dropped as a restart drops them, and the next pass (the boot's full scan, idle) republishes both rows at 3 of 3; a further full-scan pass over matching rows writes nothing. Without the full-scan check it fails (0 == 3).
    • test_current_counts_over_a_coverage_row_behind_are_republished_after_a_restart: coverage published, the previous build's vectors land, counts published; after a restart the idle full scan republishes the coverage at 3. Without reading the coverage row it fails (3 == 0).
    • test_a_coverage_row_alone_behind_the_chunks_is_republished_after_a_restart: only the coverage row is published (counts removed), the last vectors land, and after a restart the full scan republishes both rows at 3. It fails without the coverage comparison on a missing counts row (KeyError reading the counts row back).
    • test_the_comparison_and_the_republish_read_with_sequential_consistency: spies on published_chunks_behind, refresh_coverage and refresh_table_counts during a restart pass and asserts each runs under sequential_reads(). It fails without the wrapper.
    • test_a_full_scan_that_meets_unmerged_stub_versions_still_notes_the_namespace: after a restart the full scan meets one superseded stub version and is not idle; the next pass, idle and not a full scan, republishes both rows. With the check gated on idle it fails (0 == 3).
    • test_a_failed_comparison_does_not_fail_the_pass: the chunk aggregate fails during the full-scan check; the pass records one error and the next full scan republishes. Without the guard the pass raises TimeoutError.
    • On main, the first test fails (2 == 5) and the three retry cases fail (no error line, nothing republished); the removed-namespace test passes there, as nothing republishes. On this head all 6 pass.
  • tests/unit/test_workers_poll_the_work.py: the recovery fake answers the two refreshes and published_chunks_behind, the fixture resets the noted set, and the idle-pass test checks that a full-scan pass checks each namespace once and writes nothing when the rows match.
  • tests/unit/test_model_calls_switch_workers.py: the full scan's check reads only the namespace of the org left on.
  • The recovery, counts, coverage, post-step, ready-flip and model-call switch tests, CLI backfill phases republish the namespace's counts and coverage #1550's test_cli_backfill_publishes_counts.py and test_cli_embed_phase_live_stubs.py (the CLI embed phase shares _embed_rows), plus test_exception_text_ratchet.py, test_public_copy.py and test_tunable_constants.py, and every unit test that names the recovery worker or the counts rows: 378 passed on the previous head; on this head the recovery, poll, model-call switch and CLI counts files: 97 passed.
  • ruff check, ruff format --check and mypy on the changed server modules and the new test pass; scripts/gates/function_length_gate.py holds its baseline. CI runs the full unit suite.

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 wrote the diff and the tests. Checked: I reviewed the whole diff and its tests; the gate runs listed under Tests are the agent's, against this head.

  • 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) 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.

Note

Medium Risk
Changes shared index status publishing and embed-recovery idle/full-scan behavior; mistakes could leave stale embedding coverage or overwrite fresher cache rows, though sequential reads and must_land mitigate replica races.

Overview
Fixes namespaces that stayed at pre-embed chunks_embedded / searchable: false when vectors were written only by code_embed_recovery or loom code backfill --phase embed, because ingest republishes table_counts and coverage only after indexing a commit.

code_embed_recovery tracks namespaces it embedded, republishes both rows on the first idle pass after draining stubs (with retry on failure), and on periodic full scans uses new published_chunks_behind to queue namespaces whose published figures trail the chunk table—covering restarts and split coverage/counts writes.

CLI backfill always republishes at phase end (coverage first with must_land, then counts) under sequential_reads; full index runs advance git_head before publish and keep the cursor until counts land. The embed phase declares the org when there is pending work or published counts are behind.

StatusMixin adds published_chunks_behind, must_land on refresh helpers, and docs/tests for interruption, replica lag, and repair/twin runs that leave watermarks unchanged.

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

@vercel

vercel Bot commented Nov 26, 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 Nov 26, 2026 11:27pm UTC
loom-telemetry Ready Ready Preview Nov 26, 2026 11:27pm UTC

Request Review

@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.

Stale Bugbot comment from a previous run.

Comment thread server/code/app/index/ingest_pass.py Resolved
@zlareb1 zlareb1 self-assigned this Nov 26, 2026

@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.

P2 — A failed final coverage write can leave the saved coverage stale indefinitely. The pass publishes complete table_counts first, then refresh_coverage can swallow a failed write. The next idle pass sees complete counts and skips the coverage refresh. Retry until both rows reflect the completed embeddings, and test a failed coverage write followed by an idle pass. Existing coverage retry thread

With nothing pending, the embed phase declared its org and republished
whenever a `table_counts` row existed. Readers write that row: `loom code
stats` with a mistyped --org leaves a row of zeros, so the same typo on
`--phase embed` declared the org, and every idle embed run on an indexed
namespace paid the org gate, the count scans and the coverage aggregates.
It now declares only when rows are pending or the published chunk counts
differ from the chunk table. That comparison is a store read,
`CodeRepo.published_chunks_behind`, over the shared counts and the chunk
aggregate.

The index phase writes `git_head` and the backfill stamps first, then
`_close_index` compacts, records the pass and republishes last on every
exit (a full run, a run resumed at the end of its cursor, a hold, a repair,
a twin label), so the `republish` flag is gone. The cursor is cleared after
the republish, so a run interrupted while it publishes resumes at the end
and publishes then.

The republish writes the coverage row first and the counts row only once the
coverage has landed. An embed phase stopped between the two, by Ctrl-C or a
failed coverage refresh, otherwise left the counts current and the coverage
at the pre-embed vectors with a matching fingerprint, and the rerun found
nothing behind. It also reads with sequential consistency: the fingerprint
it stamps carries the `git_head` the run has just written, which the replica
serving the read may not have yet.

The tests drive `_backfill` and `_backfill_embed` against the real
`CodeRepo` over a replica that lags the run's writes, and read the rows
back.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@KaloyanNaumov
KaloyanNaumov deleted the coverage-refresh branch November 26, 2026 17:57
@KaloyanNaumov
KaloyanNaumov restored the coverage-refresh branch November 26, 2026 17:59
@KaloyanNaumov KaloyanNaumov reopened this Nov 26, 2026
@KaloyanNaumov KaloyanNaumov changed the title A pass that indexes no commit republishes the chunk counts while they lag the vectors The recovery worker republishes counts and coverage when it drains a namespace Nov 26, 2026
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Rebuilt rather than patched: the head is now one commit in code_embed_recovery. It notes each namespace it writes chunk vectors to, and its first idle pass republishes coverage and then table_counts for them. The chunk_counts row, the fingerprint gate and the short-circuit are gone, together with the "published pending is 0" snapshot that the coverage-retry finding and the review's other P2s rested on. Both refreshes now take must_land=True there, so a failed coverage write, a failed counts write or a failed aggregate keeps the namespace noted and the next idle pass retries both rows. test_a_failed_republish_is_retried_on_the_next_idle_pass covers each of the three, and each case reads the rows back. With #1550, now merged, covering the CLI phases, both writers #1556 names publish what they write; this head is cut from main after it.

@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.

Stale Bugbot comment from a previous run.

Comment thread server/code/workers/embed_recovery.py Resolved

@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.

P2 — A restart or worker handoff after embedding the last stub loses the pending counts and coverage refresh. I verified the current pass only republishes entries in its process-local set, and the replacement worker finds no live stub to put the namespace back in that set. Persist the refresh obligation or derive it from shared state. Cover a restart between the final vector write and the refresh. Existing restart finding

@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 restart regression passes when both saved rows are old. A mixed-version handoff can still leave coverage stale when the saved counts are current.

Comment thread server/code/workers/embed_recovery.py Resolved

@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.

Stale Bugbot comment from a previous run.

Comment thread server/code/workers/embed_recovery.py Resolved
Comment thread server/code/workers/embed_recovery.py Resolved
@KaloyanNaumov
KaloyanNaumov marked this pull request as draft November 26, 2026 20:49
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Converting to draft while I address it; a new head and the replies will follow, so no need to look at this head again.

The CLI writes the counts row only once the coverage row has landed, but
`refresh_coverage` swallowed its own write failure, so the counts landed
anyway and the next embed phase found nothing behind. The CLI now passes
`must_land=True`, which makes the coverage write raise.

`published_chunks_behind` also reads the coverage row's embedded figure,
so current counts over a coverage row that trails the chunks still count
as behind. That state comes from vectors landing between the two writes,
or from an earlier build that published counts after a failed coverage
write.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
@KaloyanNaumov
KaloyanNaumov marked this pull request as ready for review November 26, 2026 21:10

@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 2 potential issues.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 50e4d4f. Configure here.

Comment thread server/code/workers/embed_recovery.py Resolved
Comment thread server/code/store/index/status.py Resolved

@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 full scan now records lag before an idle pass, and the comparison covers stale coverage beside current counts. It still needs to check coverage when a counts row is missing.

Comment thread server/code/store/index/status.py Resolved

@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 earlier SCIM integration run failed while flushing TraceLogElement. The same test failed on main with ClickHouse error 159. The repair flushes query_log directly and preserves the query-work assertions. Earlier job · Main job · Repair #1495

@KaloyanNaumov
KaloyanNaumov marked this pull request as draft November 26, 2026 22:11
@KaloyanNaumov

Copy link
Copy Markdown
Contributor Author

Thanks, I've seen the new comments. Back to draft while I address them; a new head and the replies will follow.

KaloyanNaumov and others added 4 commits November 26, 2026 22:13
When the index phase published coverage and its first counts write failed,
an embed phase that landed its last vectors and stopped before publishing
left an idle rerun reading a missing counts row as nothing to republish.
published_chunks_behind now compares the coverage row on its own.

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

`code_embed_recovery` stores vectors without moving a watermark, and only an
ingest pass that indexed a commit republishes the shared `table_counts` and
`coverage` rows. A namespace whose stubs recovery filled kept its pre-embed
figures, read `searchable: false`, and was searched lexical-only until a
commit landed.

The worker now notes each namespace it wrote chunk vectors to, and its first
idle pass, the scan finding nothing left, republishes both rows for each of
them with `refresh_coverage` and `refresh_table_counts`. A failed republish
keeps the namespace noted, so the next idle pass retries it: the two refreshes
take `must_land`, which raises a failed state write instead of dropping it.
A namespace the pass did not scan, removed or switched off, waits, so a
removed one is never written after its purge.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
The namespaces whose chunks code_embed_recovery embedded are noted in process
memory, so a restart between the last vectors and the next idle pass lost the
note, and the namespace kept its pre-embed counts and coverage rows until a
commit landed.

A full-scan idle pass (the first after boot, then every
code_embed_recovery_full_scan_seconds) now compares each configured
namespace's published embedded chunk count with one aggregate of its live
chunks, and notes the namespace when they differ. The in-memory note stays the
fast path; the check is what guarantees the republish, at one point read and
one aggregate per namespace per full-scan period. No state row is added: the
published table_counts row already carries the figure, and nothing cheaper
records that vectors landed (the counts fingerprint covers watermarks only).
`shared_counts`, the read of that row, becomes public for the worker.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
The full-scan check compared only the published counts with the chunks,
so a coverage row that trailed under current counts was never noted. That
is the state a handoff leaves when the previous build's vectors land
between its coverage write and its counts write. The check now calls
`published_chunks_behind`, which reads both rows, and `shared_counts`
goes back to private.

It also ran only on an idle pass. The first full scan after a restart
reads superseded stub versions from zero and is rarely idle, and the
idle passes after it are not full scans, so the check never ran. It now
runs on every full-scan pass, and the next idle pass republishes what it
noted.

The read is guarded per namespace like the republish: a failed read is
a pass error, and the next full scan reads that namespace again.

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

A lagging replica could make a current counts or coverage row read as behind,
and let the republish overwrite it with older figures; the CLI's republish
already reads sequentially. The worker's comparison and its republish now do
too. A namespace with only a coverage row published, its counts write having
failed, is compared and republished after a restart.

Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@KaloyanNaumov
KaloyanNaumov force-pushed the coverage-refresh branch 2 times, most recently from 50e4d4f to 154f7f7 Compare November 26, 2026 23:25
@KaloyanNaumov
KaloyanNaumov marked this pull request as ready for review November 26, 2026 23:26

@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 restart and missing-counts regressions pass. A drained namespace can still stay unsearchable while another namespace has recovery work.

result.summaries_embedded += embedded
if result.full_scan:
await _note_behind(repo, set(repos) - _unpublished, result)
if result.idle:

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.

P2 — A namespace whose chunks have all been embedded still waits for the entire worker to be idle. In a focused test, three vectors landed for one namespace, then three passes found only summary work for a second namespace. The first namespace still published zero embedded chunks and zero coverage. Publish drained namespaces independently of other namespaces' backlog, and test that case.

This branch has not been deployed

No deployments
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.

An embedded namespace whose branch does not move keeps reporting zero embedding coverage and is searched lexical-only

2 participants