Skip to content

A branch added from the page reuses its source's vectors - #1966

Merged
zlareb1 merged 11 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/1680-01-branch-reuses-source-vectors
Dec 4, 2026
Merged

zlareb1 merged 11 commits into
ClickHouse:mainfrom
KaloyanNaumov:pr/1680-01-branch-reuses-source-vectors

Conversation

@KaloyanNaumov

@KaloyanNaumov KaloyanNaumov commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

A branch added from the Repositories page (code.repo.add_branch) now copies the vectors of identical chunks from the namespace it was added from, so its first index embeds only what differs. Before, only an operator's LOOM_CODE_REPOS_JSON entry could set embed_reuse_from, and a page-added branch embedded every chunk again. The registry row gets a base_namespace column that carries the source.

Related: #1680

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

A branch added from the Repositories page reuses the embeddings of identical chunks from the namespace it was added from, instead of embedding the whole branch again.

Why

code.repo.add_branch registers the branch in loom.code_repos, and row_to_config builds its RepoConfig from that row. Neither set embed_reuse_from, which only an operator entry could carry (server/code/domain/index/repos.py). bootstrap.index_slice then called ingest_file without a reuse source, so the first index embedded every chunk although the source namespace holds the same vectors. The embed-recovery worker and the indexing passes already read cfg.embed_reuse_from, so they work once the config carries it.

The fix has three parts:

  1. loom.code_repos gets base_namespace (LowCardinality(String) DEFAULT '', migration 121). code.repo.add_branch writes the namespace it was called on; code.repo.add writes ''.
  2. row_to_config maps base_namespace onto RepoConfig.embed_reuse_from. The registry snapshot read selects the column, and a removed row reads it empty, like the other fields of a removed row.
  3. bootstrap.index_slice passes cfg.embed_reuse_from to ingest_file, as reconcile and loom code backfill already do.

Scope and risk

Touches the registry row, its two readers, the first-index slice and the answer cache's trunk lookup. It leaves operator entries alone: their embed_reuse_from still comes from the JSON entry, and an operator entry's access row records no base.

The reuse covers vectors only. answers_cache._trunk_of also reads embed_reuse_from, to let a release branch borrow its trunk's cached answers, and it now reads it only for an operator entry (source == "config"). A borrowed answer is copied into the branch, and later hits on that copy check the branch alone, not the source grant, so a viewer who lacks the source grant, or loses it after the copy, would read it. A page-added branch therefore never reads its source's cached answers; it synthesizes and caches its own.

Second effect, throughput only: with a reuse source set, pass_commit skips the round-wide embedding prefetch (embed=not embed_reuse_from), so later passes embed per file, as they do for an operator branch with a source. On the first index, the slice's prefetch passes no reuse source either, so the source's vectors are read with one statement per file rather than one per round as loom code backfill does. The result is the same; only the number of reads differs.

A branch whose source namespace is later removed finds no vectors there (reusable_vectors returns nothing) and embeds its chunks.

Compatibility, rollout and rollback

  • Wire: none. No op input or output changes.
  • Schema: one additive column, schemas/migrations/121_code_repo_base_namespace.sql. A previous build reads the registry by column name and rewrites a row only by copying it whole (upsert_access copies the row read with SELECT *; deactivate_code_repo inserts SELECT * REPLACE), so it keeps a base_namespace a newer pod wrote. A previous build that adds a branch writes no value; that branch reads '' and embeds every chunk, as before. Rows written before the migration read ''.
  • Operator steps: none. A branch added earlier keeps embedding every chunk; adding a new one gets the reuse.
  • Rollback: reverting leaves the column and its values in place; the previous build ignores them.

Tests

Red on main, then green with the change (focused, on a private ClickHouse for the integration file):

  • tests/unit/test_code_repo_self_service.py::test_a_branch_added_from_the_page_embeds_no_chunk_its_source_holds adds a repository and a branch through handle_code_repo_add and handle_code_repo_add_branch, indexes the source with the real index_slice and ingest_file, then indexes the branch, whose file is the source's. On main the branch's embed_reuse_from is '' and its embedder is asked for the chunk; with the change the embedder is asked for nothing and the branch's chunk rows carry the source's vectors. With only the registry part of the change (no bootstrap edit) the same test still fails on the embedder, so it covers the slice too.
  • tests/unit/test_code_repo_self_service.py::test_row_to_config_reuses_the_vectors_of_the_namespace_a_branch_was_added_from covers the row mapping, and the existing add-branch test asserts the stored base_namespace of both rows.
  • tests/unit/test_code_ask_cache_v2.py::test_a_branch_added_from_the_page_never_serves_its_source_answers is the two-viewer case: a viewer who may read the source asks a page-added branch, then a viewer without the source grant asks the same question. With _trunk_of reading every entry, the first ask is served the source's entry and stores a copy that the second viewer then reads; with the change the first ask synthesizes and no stored row names a source. The operator release-branch reuse tests in the same file still pass.
  • tests/analyzer/shapes.txt is rewritten for the schema dump's new digest (python -m tests.analyzer.corpus --write; one shape changed).
  • tests/integration/test_code_repo_row_kind.py::test_a_branch_keeps_the_namespace_it_was_added_from_until_it_is_removed round-trips the field on ClickHouse: stored, read back as the entry's vector source, kept through an access change, empty once the branch is removed, and empty for a row written without the column.
tests/unit/test_code_repo_self_service.py::test_row_to_config_reuses_the_vectors_of_the_namespace_a_branch_was_added_from
tests/unit/test_code_repo_self_service.py::test_a_branch_added_from_the_page_embeds_no_chunk_its_source_holds   # main: 2 failed; change: 2 passed
tests/integration/test_code_repo_row_kind.py                                                                    # main: 1 failed, 5 passed; change: 6 passed

Also run: ruff format --check and ruff check, function_length_gate, check_no_secrets, mypy_gate, docs_reference --check, op_reach_census --check, make schema-check (the dump differs from main in the one added line), and the unit files for the add paths, bootstrap, the release-branch config, schema baseline, additive migrations, table settings, doc links and public copy. make test-mixed-version needs docker and is left to CI.

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 read the diff and ran the tests above.

  • 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: schemas/schema.sql (make schema-dump); no op, setting or plugin file changed.
  • 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 bumps both plugin manifests' version: not applicable, none changed.
  • The frontend builds and its tests pass, if frontend/ changed: not applicable, unchanged.
  • 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 indexing embed reuse, registry schema, and ask-cache trunk borrowing; answer-cache gating reduces cross-namespace leakage but behavior differs for page vs operator branches.

Overview
Branches added via code.repo.add_branch now record the source namespace in base_namespace on loom.code_repos and map it to RepoConfig.embed_reuse_from, so the first index and ingest path copy vectors for identical chunks instead of re-embedding the whole branch. bootstrap.index_slice passes that reuse source into ingest_file, matching backfill/reconcile behavior operators already had via config.

Answer-cache cross-branch reuse is tightened: _trunk_of only treats embed_reuse_from as a trunk for operator entries (source == "config"). Page-added branches still reuse embeddings from the source but do not serve or copy the source’s cached code.ask answers, avoiding cases where a branch cache could leak trunk answers to callers who lost trunk access.

Docs and ops guides describe the new default for Repositories-page branches versus manual embed_reuse_from setup. Migration 121 adds the column additively with DEFAULT '' for backward compatibility.

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

code.repo.add_branch registered the branch in loom.code_repos without a
vector source, and only an operator entry could set embed_reuse_from, so a
branch added from the page embedded every chunk its source namespace
already held.

loom.code_repos records the namespace a branch was added from
(base_namespace, migration 118). row_to_config maps it onto
embed_reuse_from, the registry read selects it, and the first-index slice
passes it to ingest_file as the indexing passes and the backfill do.

answers_cache reads embed_reuse_from too, so such a branch now also reads
its source's cached answers, behind the existing access and file-hash
checks.

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

vercel Bot commented Dec 1, 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 4, 2026 8:13am UTC
loom-telemetry Ready Ready Preview Dec 4, 2026 8:13am UTC

Request Review

@zlareb1 zlareb1 self-assigned this Dec 1, 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.

The registry field also turns on source answer reuse for branches added from the Repositories page. A copied answer can outlive its source access check.

Comment thread server/code/app/index/repo_registry.py Resolved
KaloyanNaumov and others added 2 commits December 1, 2026 23:49
The statement corpus renders schemas/schema.sql as one shape, and the
dump gained the base_namespace column, so the committed shape list named
the old digest. Rewritten with python -m tests.analyzer.corpus --write;
the only change is that one shape.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
answers_cache._trunk_of read embed_reuse_from for every entry, so a page
branch, whose registry row now sets it, borrowed its source's cached
answers. A borrowed answer is copied into the branch, and later hits on
the copy check the branch alone, not the source grant: a viewer who lacks
that grant, or loses it after the copy, would read it.

_trunk_of now reads embed_reuse_from only for an operator entry. A page
branch takes vectors from its source and synthesizes its own answers.

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

Copy link
Copy Markdown
Contributor Author

CI: pytest integration isolation gates

This was ours. The analyzer corpus hashes schemas/schema.sql as one statement shape, and the added base_namespace column changed its digest, so tests/analyzer/shapes.txt named the old one. Rewritten with python -m tests.analyzer.corpus --write in c521e7c; the diff is that one shape, and test_shapes_file_matches_builders passes.

zlareb1
zlareb1 previously approved these changes Dec 2, 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.

Branches added from the Repositories page copy matching vectors and use their own answer cache. The two-viewer regression and 223 other focused tests passed.

@zlareb1

zlareb1 commented Dec 2, 2026

Copy link
Copy Markdown
Member

Fix merge conflicts

Main took migration number 118 for 118_store_names_reserved.sql, so this
branch's 118_code_repo_base_namespace.sql is now 119. Its content is
unchanged; test_schema_baseline.py lists both files in order.

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

Copy link
Copy Markdown
Contributor Author

Merged main (97a2a81) into the branch; one conflict.

tests/unit/test_schema_baseline.py listed the migration files, and both sides had added a 118: main's 118_store_names_reserved.sql and this branch's 118_code_repo_base_namespace.sql. Main's keeps 118. This branch's file is now 119_code_repo_base_namespace.sql with its content unchanged, and the list names both in order. Nothing else referred to the old number.

The schema dump, docs and code merged without a conflict. The row-to-config, answers cache and self-service tests pass, as do the schema baseline, journal and numeric-order tests.

zlareb1
zlareb1 previously approved these changes Dec 2, 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.

The migration and journal use 119, and the 224 focused tests pass. Update the two references to migration 118 in the PR description before the squash merge.

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

GitHub reports a merge conflict in tests/unit/test_schema_baseline.py. Please reconcile the migration baseline with main so CI can test the combined tree.

Main took migration 119 for workspace crawl, so the base namespace
column moves to 120_code_repo_base_namespace.sql, and the journal list
in the baseline test names both.

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

Copy link
Copy Markdown
Contributor Author

Round

Merged main in f8862bd. Main took migration 119 for workspace crawl, so the base namespace column moves to 120_code_repo_base_namespace.sql, and test_the_journal_files lists both. A ClickHouse migrated from empty applies 119 and then 120, and the schema it builds matches schemas/schema.sql for our tables: the two migrations touch different tables. Two unrelated view columns (value_withheld) print a different type on this ClickHouse build, as they do for main. test_schema_baseline.py, test_table_settings.py, test_code_repo_self_service.py and tests/integration/test_code_repo_row_kind.py pass on the merge. Ready for another look.

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

Migration 120 follows the workspace crawl migration and the schema baseline includes both. The vector reuse and answer-cache changes match the previously reviewed implementation. Before squash merge, update both references to migration 118 in the PR description.

@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 analyzer shape test fails on the exact CI merge and passes on its first parent. Update the snapshot for migration 120 and update the two migration numbers in the PR description. Failed analyzer job

Comment thread tests/analyzer/shapes.txt Resolved
Signed-off-by: Kaloyan Naumov <knaumov@gmail.com>
Regenerated with python -m tests.analyzer.corpus --write on the merge of upstream main bd58b7b. The schema dump shape becomes 5201d159797b; 3e3256030728 and c25cec2435d6 are gone.

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

Copy link
Copy Markdown
Contributor Author

Steady point: 3430a33d8.

The snapshot is regenerated, not edited: main was merged first (it had added analyzer cases since the head), then

python -m tests.analyzer.corpus --write
# wrote 2344 shapes

and the diff is exactly what you named — +5201d159797b, −3e3256030728, −c25cec2435d6. pytest tests/analyzer: 10 passed, 2 skipped.

The body is updated from migration 118 to 120 in both places.

@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 analyzer snapshot carries the schema with base_namespace. The analyzer and schema checks passed. The PR description still names migration 118 in two places. Use 120_code_repo_base_namespace.sql before merging.

@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 pinned Biome check finds the same unformatted dashboard assertion in this run and on clean main. The separate repair formats that assertion. Failed job · Repair #2107

@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 branch migration conflicts with the migration number already on main. GitHub also reports a conflict in the migration baseline.

main took 120 for usage-label provenance, so this file becomes 121 and the
journal baseline lists it after it.
…rations

# Conflicts:
#	tests/unit/test_schema_baseline.py
@cla-assistant

cla-assistant Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@KaloyanNaumov
KaloyanNaumov force-pushed the pr/1680-01-branch-reuses-source-vectors branch from b7aa6e2 to 6f1bb49 Compare December 4, 2026 08:11

@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 branch copies vectors only for matching chunks in the same organization and keeps cached answers scoped to the branch. Migration 121 follows the current migration journal and preserves the field when an older build copies a registry row.

@zlareb1
zlareb1 merged commit 9d374f2 into ClickHouse:main Dec 4, 2026
78 of 80 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.

2 participants